在Checkbox.Checked或Unchecked上执行命令

Hos*_*146 40 wpf checkbox command mvvm

我在窗口上有一个复选框控件.我想执行一个命令来调用相关视图模型中的方法.我还需要复选框的值.我似乎找不到将命令与复选框关联的方法.有没有人这样做过?

Ars*_*eny 68

<CheckBox Content="CheckBox"
          Command="{Binding YourCommand}"
          CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />
Run Code Online (Sandbox Code Playgroud)

  • ...使用`CommandParameter ="{Binding IsChecked,RelativeSource = {RelativeSource Self},Mode = OneWay}将CommandParameter绑定到CheckBox的值 (30认同)
  • ...并使用RelativeSource.Self将CommandParameter绑定到CheckBox的值 (10认同)

Igo*_*r S 23

如果您使用MVVM,则可以使用如下事件触发器:

<CheckBox IsChecked="{Binding ServiceOrderItemTask.IsCompleted, Mode=TwoWay}" Content="{Binding ServiceOption.Name}">

    <i:Interaction.Triggers>
          <i:EventTrigger EventName="Checked">
                 <i:InvokeCommandAction Command="{Binding DataContext.IsCompletedCheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type t:RadGridView}}}" CommandParameter="{Binding}"/>
           </i:EventTrigger>

           <i:EventTrigger EventName="Unchecked">
                 <i:InvokeCommandAction Command="{Binding DataContext.IsCompletedUncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type t:RadGridView}}}" CommandParameter="{Binding}"/>
           </i:EventTrigger>
    </i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

  • 非常好,但为了使用它,您需要在XAML中使用这些命名空间:`xmlns:t ="http://schemas.telerik.com/2008/xaml/presentation"``xmlns:i ="http:// schemas .microsoft.com /表达/ 2010 /交互"` (4认同)
  • 以及如何在视图模型中实现? (2认同)

Roh*_*ats 11

这将符合您的要求 -

<CheckBox CommandParameter="{Binding}"
          Command="{Binding DataContext.AddRemovePresetAssignmentCommand,
          RelativeSource={RelativeSource FindAncestor,
                           AncestorType={x:Type UserControl}}}"
          Content="{Binding Path=Name}">
Run Code Online (Sandbox Code Playgroud)


kub*_*b1x 5

  • 添加System.Windows.Interactivity到您的项目参考。
  • 添加xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"到您的 XAML 命名空间。
<CheckBox IsChecked="{Binding SomeBoolProperty, Mode=OneWay}" Content="Check Meee!">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Checked">
            <i:InvokeCommandAction Command="{Binding MyOnCheckedCommand}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Unchecked">
            <i:InvokeCommandAction Command="{Binding MyOnUncheckedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</CheckBox>
Run Code Online (Sandbox Code Playgroud)

我在 ViewModel 上实现INotifyPropertyChanged如下:

<CheckBox IsChecked="{Binding SomeBoolProperty, Mode=OneWay}" Content="Check Meee!">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Checked">
            <i:InvokeCommandAction Command="{Binding MyOnCheckedCommand}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Unchecked">
            <i:InvokeCommandAction Command="{Binding MyOnUncheckedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</CheckBox>
Run Code Online (Sandbox Code Playgroud)

我的ViewModelSomeBoolProperty看起来像这样:

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
Run Code Online (Sandbox Code Playgroud)

我使用 RelayCommand 作为这里的命令实现 /sf/answers/1560077151/

我的 ViewModel 上的命令如下所示:

private bool _SomeBoolProperty = false;
public bool SomeBoolProperty { 
    get => _SomeBoolProperty;
    set { 
        _SomeBoolProperty = value; 
        OnPropertyChanged(nameof(SomeBoolProperty)); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

我遇到这个问题时试图找到一种方法来重用我的 ViewModel 上已有的两个命令。一个在选中时调用,一个在未选中时调用。我也在一些按钮上使用它们,所以不想添加额外的参数化命令。人们在这里询问 ViewModel 实现,因此添加此答案来完成Igor_S的答案。希望能帮助到你。