我已经重现了你的问题。但一切正常。
问题可能是GroupName
单选按钮的属性没有定义。为两个单选按钮的 GroupName 指定相同的名称。
前任:
<RadioButton Content="No" GroupName="group1" />
<RadioButton Content="Yes" GroupName="group1" />
这是我的完整 XAML:
<Window x:Class="RadioButtonFocus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="350" Width="525">
<Grid Name="mainGrid">
<StackPanel Orientation="Horizontal">
<RadioButton Content="No" GroupName="group1" IsChecked="{Binding IsNoChecked}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding CheckNoCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="Yes" GroupName="group1" IsChecked="{Binding IsYesChecked}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding CheckYesCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</StackPanel>
</Grid>
</Window>
视图模型
public class MainViewModel : ViewModelBase
{
#region Declarations
private bool isNoChecked;
private bool isYesChecked;
private ICommand checkNoCommand;
private ICommand checkYesCommand;
#endregion
#region Properties
/// <summary>
/// Gets or sets a value indicating whether this instance is no checked.
/// </summary>
/// <value>
/// <c>true</c> if this instance is no checked; otherwise, <c>false</c>.
/// </value>
public bool IsNoChecked
{
get
{
return isNoChecked;
}
set
{
isNoChecked = value;
NotifyPropertyChanged("IsNoChecked");
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is yes checked.
/// </summary>
/// <value>
/// <c>true</c> if this instance is yes checked; otherwise, <c>false</c>.
/// </value>
public bool IsYesChecked
{
get
{
return isYesChecked;
}
set
{
isYesChecked = value;
NotifyPropertyChanged("IsYesChecked");
}
}
#endregion
#region Commands
/// <summary>
/// Gets the check no command.
/// </summary>
/// <value>The check no command.</value>
public ICommand CheckNoCommand
{
get
{
if (checkNoCommand == null)
{
checkNoCommand = new RelayCommand(param => this.CheckNo(),
null);
}
return checkNoCommand;
}
}
/// <summary>
/// Gets the check yes command.
/// </summary>
/// <value>The check yes command.</value>
public ICommand CheckYesCommand
{
get
{
if (checkYesCommand == null)
{
checkYesCommand = new RelayCommand(param => this.CheckYes(),
null);
}
return checkYesCommand;
}
}
#endregion
#region Private Methods
/// <summary>
/// Changes the checked.
/// </summary>
private void CheckNo()
{
this.IsNoChecked = true;
}
/// <summary>
/// Checks the yes.
/// </summary>
private void CheckYes()
{
this.IsYesChecked = true;
}
#endregion
}
注意:这里我使用了交互库。您必须安装可再发行组件才能运行此示例。你可以从这里下载