我想我已经按照此post中给出的示例进行了操作,但是当按钮更改时我的属性没有改变。关于我哪里出错的任何建议?
枚举和类的C#代码
public enum SystemTypes
{
TypeA,
TypeB
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SystemTypes systemType = SystemTypes.TypeA;
public SystemTypes SystemType
{
get { return systemType; }
set { systemType = value; }
}
}
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
XAML
<Canvas>
<Canvas.Resources>
<local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />
</Canvas.Resources>
<RadioButton x:Name="TypeARadioButton" Content="TypeA" Canvas.Left="10" Canvas.Top="10"
IsChecked="{Binding Path=SystemType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:SystemTypes.TypeA}}" />
<RadioButton x:Name="TypeBRadioButton" Content="TypeB" Canvas.Left="10" Canvas.Top="31"
IsChecked="{Binding Path=SystemType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:SystemTypes.TypeB}}" />
</Canvas>
答案 0 :(得分:1)
你需要将Binding Mode设置为TwoWay,然后在转换器实现方法ConvertBack中负责将bool转换为SystemTypes,在SystemType的清除器中包含
set { systemType = value; OnPropertyChanged(() => "SystemType");}
为了填补属性,其值已更改。
OnPropertyChanged(() => "SystemType")
如果您实现接口INotifyPropertyChanged,可以工作。我不能你是否设置DataContext,如果你没有绑定不工作。为了在InitializeComponent()add
之后纠正这个问题this.DataContext = this;