MVVM 超级解耦后,控件的不同事件的命令绑定
通过MVVM框架,我们可以很好的实现View跟具体业务逻辑的解耦,之前在项目中,通过运用Command,将View端的.cs完全解放了出来,实现了超级解耦,然后这里面大多我用到的Command是针对Button按钮而言的,由于只用到了Button的Click事件,直接通过Command的传递到ViewModel中,我也就没有深究其理。随后在项目中,由于用到DataGrid控件,用到控件中的某些具体事件,比如SelectionChanged,我依旧试图通过Command实现事件的命令传递,然后直到这个时候,我才发现了一个大的问题,那就是Command怎么知道我该传递什么事件呢,毕竟像SelectionChanged这样的事件,一个控件DataGrid种包含十几个这样不同的事件,那么问题来了,我该如何解决呢,所以知道这个时候,我才开始反思也许之前Button的命令Command传递成功了,只是一个特例,就是在Button按钮中,Command不传递任何参数时,默认传递Click事件,这个猜测是否对呢,答案是对的。
那么,到底该如何解决的这个不同事件的命令传递呢,通过我不断尝试与在网上寻找资料,终于找到了答案。那就是运用EventToCommand
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
比如:
<Button> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseEnter" > <cmd:EventToCommand Command="{Binding FooCommand}" CommandParameter=""/> </i:EventTrigger> </i:Interaction.Triggers> </Button>
而在DataGrid中的运用如下:
<DataGrid Name="dOP1" SelectedItem="{Binding SelectedOutPortItem}" ItemsSource="{Binding TabOutPort1Source}"> <DataGrid.Columns> <DataGridTextColumn Header="{StaticResource UcUnitAdjust_dtSensorOrPort_Col1}" Width="3*" Binding="{Binding Name}" ElementStyle="{StaticResource dataGridElement}"> </DataGridTextColumn> <DataGridTemplateColumn Width="*" HeaderTemplate="{StaticResource statusHead}" CellTemplate="{StaticResource statusCell}"> </DataGridTemplateColumn> </DataGrid.Columns> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <command:EventToCommand Command="{Binding SelectionChangedCommand}" CommandParameter=""/> </i:EventTrigger> </i:Interaction.Triggers> </DataGrid>
public RelayCommand SelectionChangedCommand { get; set; } SelectionChangedCommand = new RelayCommand(ExecuteSelectionChanged); private void ExecuteSelectionChanged() { Sensor posItem = SelectedOutPortItem as Sensor; if (posItem == null) return; IsEnabled_btnBackward = false; IsEnabled_btnForward = false; Content_btnBackward = LangGet.GetMessage("UcUnitAdjust_btnForward1"); Content_btnForward = LangGet.GetMessage("UcUnitAdjust_btnBackward1"); string strRid = posItem.PortID; if (strRid == "0x65010101" || strRid == "0x65010201" || strRid == "0x65011701" || strRid == "0x65011801" || strRid == "0x65012c01" || strRid == "0x65012d01" || strRid == "0x65013201" || strRid == "0x65013301" || strRid == "0x65014101" || strRid == "0x65014201" || strRid == "0x65014801" || strRid == "0x65014901" ) { IsEnabled_btnBackward = true; IsEnabled_btnForward = true; if (strRid == "0x65010101" || strRid == "0x65010201" || strRid == "0x65012c01" || strRid == "0x65012d01" || strRid == "0x65014801" || strRid == "0x65014901" ) { Content_btnForward = LangGet.GetMessage("UcUnitAdjust_btnForward2"); Content_btnBackward = LangGet.GetMessage("UcUnitAdjust_btnBackward2"); } else if (strRid == "0x65011701" || strRid == "0x65011801") { Content_btnForward = LangGet.GetMessage("UcUnitAdjust_btnForward3"); Content_btnBackward = LangGet.GetMessage("UcUnitAdjust_btnBackward3"); } else if (strRid == "0x65013201" || strRid == "0x65013301") { Content_btnForward = LangGet.GetMessage("UcUnitAdjust_btnForward4"); Content_btnBackward = LangGet.GetMessage("UcUnitAdjust_btnBackward4"); } else if (strRid == "0x65014101" || strRid == "0x65014201") { Content_btnForward = LangGet.GetMessage("UcUnitAdjust_btnForward5"); Content_btnBackward = LangGet.GetMessage("UcUnitAdjust_btnBackward4"); } } }

您的资助是我最大的动力!
金额随意,欢迎来赏!

我写的东西能让你能懂,那是义务
毕竟占用了你生命中的宝贵的时间和注意力
要是你还能喜欢我的作品,那就是缘分了
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【青青子衿】!