我无法将两个具有相同分组名称的单选按钮与XAML表单和IsChecked属性绑定起来,我还使用了NullableToBoolConverters。但是,在我的代码中,单选按钮的IsChecked属性不会改变(一旦我们点击第一个单选按钮后,它就根本没有遇到断点),我需要分别绑定两个单选按钮的IsChecked属性,因为我需要基于单选按钮属性设置表单上其他面板的可见性。
以下是我的XAML代码,后面是用于RadioButton属性的ViewModel.cs代码:
以下是我的转换器代码:
<RadiobuttonSelectedConverter x:Key="CheckedSelection"/>// declaring my converter class in my resource dictionary.
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="3" Margin="10,5,0,0">
<RadioButton GroupName="RadiosGroup" IsChecked="{Binding IsRadioButton1,Mode=TwoWay,
Converter={StaticResource CheckedSelection}, ConverterParameter=true}">
First</RadioButton>
<RadioButton GroupName="RadiosGroup"
Margin="40,0,0,0" IsChecked="{Binding IsRadioButton2,Mode=TwoWay,
Converter={StaticResource CheckedSelection}, ConverterParameter=true}">Second</RadioButton>
</StackPanel>
private bool _isRadioButton1;
public bool IsRadioButton1
{
get
{
return _isRadioButton1;
}
set
{
if _isRadioButton1!= value)
{
_isRadioButton1= value;
IsRadioButton2= false;
OnPropertyChanged("IsRadioButton1");
}
}
}
private bool _isRadioButton2;
public bool IsRadioButton2
{
get
{
return _isRadioButton2;
}
set
{
if (_isRadioButton2 != value)
{
_isRadioButton2 = value;
IsRadioButton1= false;
OnPropertyChanged("IsRadioButton2");
}
}
}
以下是我的转换器代码:
[ValueConversion(typeof(bool?), typeof(bool))]
public class RadiobuttonSelectedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(parameter.ToString());
if (value == null)
{
return false;
}
else
{
return !((bool)value ^ param);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(parameter.ToString());
return !((bool)value ^ param);
}
}
有人能帮我解决问题吗?提前感谢。
value == param
? - Daniel Hilgarthbool
类型,它不能为null
,那么您为什么要首先使用Converter
呢?如果没有转换器,您的代码是否能正常工作? - Rachel