Silverlight MVVM:我的(object sender,RoutedEventArgs e)在哪里? [英] Silverlight MVVM: where did my (object sender, RoutedEventArgs e) go?
问题描述
XAML
< i:Interaction.Triggers>
< i:EventTrigger EventName =点击>
< i:InvokeCommandAction Command ={Binding mvvmButtonclick}/>
< / i:EventTrigger>
< / i:Interaction.Triggers>
Viewmodel代码
public ICommand mvvmButtonclick
{
get;
私人集;
}
Viewmodel用于连接命令的构造方法代码 p>
this.mvvmButtonclick = new ActionCommand(this.ButtonClickedEvent);
在按钮点击中调用的viewmodel中的实际方法
private void ButtonClickedEvent()
{
MessageBox.Show(working !!!);
}
这个工作。所以我的问题是:
- 这是正确的吗?
- 有没有办法将(object sender,RoutedEventArgs e)参数传播到我的viewmodel中,我应该关心它不在吗?
- 假设这是一个列表框选择更改事件,而不是一个按钮单击。
我想你可能会错过交互触发器旨在提供的视图和视图模型之间的分离点。
交互触发器的目的是允许设计者(通常使用Blend)在视图模型上调用命令。 UI元素和UI元素中的哪个事件可能会调用这样的命令是设计师选项。
如果ViewModel确实需要在这样一个命令的调用期间,将提供 EventArgs
的特定导数,这样就可以使设计师的手一点点。它将创建交互触发想要消除的视图和视图模型之间的耦合。
关于您的最后一个问题,确定当前选定项目的方式在列表框中,当更改时将被通知,将在视图模型上创建绑定到ListBox的 SelectedItem
的属性。不需要员工互动触发器或这类事情的命令。
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?
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.
这篇关于Silverlight MVVM:我的(object sender,RoutedEventArgs e)在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!