我想我已经遵循了本给出的例子后,但是当按钮被改变我的属性没有改变。有什么建议可以解决我的问题吗?
枚举和类的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;
}
}
a
<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>
您需要将Binding Mode设置为TwoWay,然后在Converter实现方法中将Convert转换为SystemTypes的ConvertBack方法,在SystemType的settter中包括
set { systemType = value; OnPropertyChanged(() => "SystemType");}
为了填充属性,其值已更改。
OnPropertyChanged(() => "SystemType")
如果实现接口INotifyPropertyChanged,则可以正常工作。我无法确定是否设置DataContext,如果未绑定则无法正常工作。为了纠正这个问题,在InitializeComponent()之后添加
this.DataContext = this;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句