C# 是否有办法将单选按钮的选中事件绑定到ViewModel中的命令?
我试过了C# 是否有办法将单选按钮的选中事件绑定到ViewModel中的命令?,c#,xaml,mvvm,command,windows-10,C#,Xaml,Mvvm,Command,Windows 10,我试过了 <RadioButton Content="Boom" Command={Binding MyCommand} IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource Mode=Self}}"/> 但什么也没发生。这是为什么?如何修复它?以下代码按预期工作: <RadioButton Cont
<RadioButton Content="Boom" Command={Binding MyCommand} IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource Mode=Self}}"/>
但什么也没发生。这是为什么?如何修复它?以下代码按预期工作:
<RadioButton Content="Boom" Command="{Binding MyCommand}" />
命令方法示例:
private ICommand _MyCommand;
public ICommand MyCommand
{
get { return _MyCommand ?? (_MyCommand = new DelegateCommand(a => MyCommandMethod(a))); }
}
private void MyCommandMethod(object item)
{
Console.WriteLine("Chosen element: " + (string)item);
}
步骤1:添加
System.Windows.Interactivity
reference
步骤2:在XAMLxmlns:i=”中添加名称空间http://schemas.microsoft.com/expression/2010/interactivity“
步骤3:
<RadioButton Content="Boom" IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource Mode=Self}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="Boom" IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource Mode=Self}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>