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");
                }
            }
           
        }

 

转载于:https://www.cnblogs.com/jiangyan219/articles/9212526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值