如果你想使用RadioButton,你只需做一些小的调整来解决RadioButton的默认行为。
您需要解决的第一个问题是基于它们的通用直接父容器自动对RadioButton进行分组。既然你不喜欢“GroupName”破解你的另一种选择是将每个RadioButton放在它自己的Grid或其他容器中。这将使每个按钮成为其自己组的成员,并会强制他们根据其IsChecked绑定行为。
<StackPanel Orientation="Horizontal">
<Grid>
<RadioButton IsChecked="{Binding Path=CurrentMode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Idle}">Idle</RadioButton>
</Grid>
<Grid>
<RadioButton IsChecked="{Binding Path=CurrentMode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Active}">Active</RadioButton>
</Grid>
<Grid>
<RadioButton IsChecked="{Binding Path=CurrentMode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Disabled}">Disabled</RadioButton>
</Grid>
<Grid>
<RadioButton IsChecked="{Binding Path=CurrentMode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Running}">Running</RadioButton>
</Grid>
</StackPanel>
这使我想到这是确保点击它后的按钮点击不留其选中状态下一个解决方法这是必要的,以触发集合调用,因为你是在装订器isChecked属性。您需要发送一个额外的NotifyPropertyChanged,但它必须被推入调度线程的队列中,以便该按钮将接收通知并更新其可视化IsChecked绑定。添加到您的视图模型类,这可能是替换现有的NotifyPropertyChanged实现和我假设你的类实现它在问题的代码所缺少的INotifyPropertyChanged的:
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
Dispatcher uiDispatcher = Application.Current != null ? Application.Current.Dispatcher : null;
if (uiDispatcher != null)
{
uiDispatcher.BeginInvoke(DispatcherPriority.DataBind,
(ThreadStart)delegate()
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
});
}
}
}
在CURRENTMODE的二传手呼吁NotifyPropertyChanged
然后(“CURRENTMODE “)。您可能已经需要类似这样的事情,因为您的服务器的ModeChanged调用可能是在不是Dispatcher线程的线程中进入的。
最后,如果您希望它们具有不同的Checked/Unchecked外观,您将需要将样式应用于您的RadioButtons。快速谷歌搜索WPF RadioButton ControlTemplate最终出现在这个网站:http://madprops.org/blog/wpf-killed-the-radiobutton-star/。