wpf checked绑定命令
时间: 2023-09-30 07:04:22 浏览: 306
在 WPF 中,可以使用 `Command` 属性将命令与 `CheckBox` 的 `Checked` 和 `Unchecked` 事件绑定。下面是一个示例:
1. 首先,在 ViewModel 中创建一个 `ICommand` 属性,用于处理 `CheckBox` 的 `Checked` 和 `Unchecked` 事件。
```csharp
public class MyViewModel
{
public ICommand CheckBoxCommand { get; set; }
public MyViewModel()
{
CheckBoxCommand = new RelayCommand<bool>(CheckBoxCommandExecute);
}
private void CheckBoxCommandExecute(bool isChecked)
{
// 处理 CheckBox 的 Checked 和 Unchecked 事件
}
}
```
2. 在 XAML 中,使用 `Command` 属性将 `CheckBox` 的 `Checked` 和 `Unchecked` 事件绑定到 ViewModel 中的 `CheckBoxCommand` 属性。
```xaml
<CheckBox Content="CheckBox" IsChecked="{Binding IsChecked}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding CheckBoxCommand}" CommandParameter="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding CheckBoxCommand}" CommandParameter="False"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
```
在上面的示例中,使用了 `RelayCommand` 类来实现 `ICommand` 接口。`RelayCommand` 会将 `CheckBox` 的 `Checked` 和 `Unchecked` 事件传递给 ViewModel 中的 `CheckBoxCommandExecute` 方法。`CommandParameter` 属性用于传递 `CheckBox` 的状态。
阅读全文
相关推荐
















