Silverlight MVVM:我的(对象发送者,RoatedEventArgs e)去了哪里?
我在视图模型中使用命令来处理事件。例如,我正在处理这样的按钮单击事件:
XAML
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding mvvmButtonclick}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Viewmodel 代码
public ICommand mvvmButtonclick
{
get;
private set;
}
Viewmodel 构造函数代码来连接命令
this.mvvmButtonclick = new ActionCommand(this.ButtonClickedEvent);
实际方法单击按钮时调用的视图模型
private void ButtonClickedEvent()
{
MessageBox.Show("worked!!!");
}
这有效。所以我的问题是:
- 这是正确的方法吗?
- 有没有一种方法可以将 (object sender, RoutedEventArgs e) 参数传播到我的视图模型中,我应该关心它是否不存在?
- 假设这是一个列表框选择更改事件而不是按钮单击。如何在没有对象发送者、SelectionChangedEventArgs e参数的情况下获取所选项目的值?
I am using commanding in my viewmodel to handle events. like for example I am handling a button click event like this:
XAML
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding mvvmButtonclick}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Viewmodel Code
public ICommand mvvmButtonclick
{
get;
private set;
}
Viewmodel Constructor code to wireup the command
this.mvvmButtonclick = new ActionCommand(this.ButtonClickedEvent);
Actual method in the viewmodel that gets called on button click
private void ButtonClickedEvent()
{
MessageBox.Show("worked!!!");
}
This works. So my questions are:
- Is this the correct way?
- Is there a way I can propogate the (object sender, RoutedEventArgs e) parameters into my viewmodel and should I care if its not there?
- Suppose if this were a listbox selection changed event and not a button click. How do I get the value of the selected item without the object sender, SelectionChangedEventArgs e parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可能错过了交互触发器旨在提供的视图和视图模型之间的分离点。
交互触发器的目的是允许设计者(通常使用 Blend)在视图模型上调用命令。哪个 UI 元素以及 UI 元素上的哪个事件可能调用此类命令是设计者的选择。
如果 ViewModel 确实要求在调用命令期间提供
EventArgs
的特定派生,那么这会稍微束缚设计人员的手脚。它将在视图和视图模型之间创建交互触发器渴望消除的那种耦合。至于您的最后一个问题,确定列表框中当前选定的项目并在其更改时收到通知的方法是在视图模型上创建一个属性,该属性绑定到列表框中的
SelectedItem
列表框。此类事情不需要员工交互触发器或命令。I think you may be missing the point of the separation between view and view-model that the interaction triggers are designed to provide.
The purpose of the interaction triggers is to allow the designer (typically using Blend) to invoke a command on the view model. Which UI element and which event on the UI element might invoke such a command is the designers choice.
If the ViewModel though did require that a specific derivative of the
EventArgs
be provided during such a call to a command then that would tie the designers hands a little. It would create the sort of coupling between the view and view-model that interaction triggers aspires to eliminate.As to your final question, the way to determine the currently selected item in a list box and be notified when it changes would be to create a property on the view model that is the bound to the
SelectedItem
of the ListBox. There is no need to employee interaction triggers or commands for this sort of thing.有一些框架(例如 Catel)允许转发 EventArgs。例如,请参阅以下实现:
http://catel.codeplex.com/SourceControl/changeset/view/508b404f2c57#src%2fCatel.Windows35%2fMVVM%2fCommands%2fCommand.cs
该类与 Silverlight 和 WPF 兼容。
如果您不想使用 EventArgs,则需要在 ViewModel 上创建一个 SelectedObject 属性并将其绑定到列表框的 SelectedItem。
Catel 包括 2 个示例应用程序(均在 WPF 和 Silverlight 中),它们使用 MVVM 和 EventToCommand 类来编辑列表框中的选定项目。我想这就是您正在寻找的!
There are some frameworks (such as Catel), that allow the forwarding of the EventArgs. For example, see this implementation:
http://catel.codeplex.com/SourceControl/changeset/view/508b404f2c57#src%2fCatel.Windows35%2fMVVM%2fCommands%2fCommand.cs
The class is compatible with both Silverlight and WPF.
If you don't want to use the EventArgs, you will need to create a SelectedObject property on the ViewModel and bind it to the SelectedItem of the listbox.
Catel includes 2 example applications (both in WPF and Silverlight) that use MVVM and the EventToCommand class to edit a selected item in a listbox. I think that is what you are looking for!