交互触发命令WPF MVVM未触发

时间:2018-11-01 15:23:54

标签: c# wpf mvvm telerik telerik-grid

我试图像下面那样将命令绑定到 RadGridView的列标题中出现的复选框

<telerik:GridViewCheckBoxColumn DataMemberBinding="{Binding IsSelected, Mode=TwoWay}" Width="85" AutoSelectOnEdit="True" EditTriggers="CellClick">                                                
  <telerik:GridViewCheckBoxColumn.Header>
     <CheckBox Command="{Binding ShowAllInDTCClickedCommand}" Content="Sh">
         <i:Interaction.Triggers>
              <i:EventTrigger EventName="Checked">                                                                
                  <i:InvokeCommandAction Command="{Binding ShowAllInDTCCheckedCommand}"/>
              </i:EventTrigger>
              <i:EventTrigger EventName="Unchecked">                                                                
                  <i:InvokeCommandAction Command="{Binding ShowAllInDTCUncheckedCommand}"/>
              </i:EventTrigger>
         </i:Interaction.Triggers>
      </CheckBox>
   </telerik:GridViewCheckBoxColumn.Header>
</telerik:GridViewCheckBoxColumn>

并尝试在 ViewModel 实现,如下所示。

    public DelegateCommand ShowAllInDTClickedCommand { get; set; }
    public DelegateCommand ShowAllInDTCCheckedCommand { get; set; }
    public DelegateCommand ShowAllInDTCUncheckedCommand { get; set; }


    ShowAllInDTClickedCommand = new DelegateCommand(ShowAllInDTClicked);
    ShowAllInDTCCheckedCommand = new DelegateCommand(ShowAllInDTCChecked);
    ShowAllInDTCUncheckedCommand = new DelegateCommand(ShowAllInDTCUnchecked);

    private void ShowAllInDTClicked()
    {
        //Do Something
    }

    private void ShowAllInDTCChecked()
    {
        //Do Something
    }

    private void ShowAllInDTCUnchecked()
    {
        //Do Something
    }                  

但是这些命令无法执行,即根本无法访问代码。 我缺少什么?

1 个答案:

答案 0 :(得分:1)

如果在视图模型中定义了DelegateCommand属性,则应这样绑定它们:

<i:EventTrigger EventName="Checked">
    <i:InvokeCommandAction Command="{Binding DataContext.ShowAllInDTCCheckedCommand, 
                                RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}"/>
</i:EventTrigger>