绑定单选按钮到枚举属性 [英] Binding radio button to enum property

查看:271
本文介绍了绑定单选按钮到枚举属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经遵循了这个发布中给出的示例,但是当按钮被更改时,我的属性没有改变。任何关于我出错的建议?



枚举和类的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)?参数:Binding.DoNothing;
}
}

xaml

 < Canvas> 
< Canvas.Resources>
< local:EnumToBooleanConverter x:Key =EnumToBooleanConverter/>
< /Canvas.Resources>
< RadioButton x:Name =TypeARadioButtonContent =TypeACanvas.Left =10Canvas.Top =10
IsChecked ={Binding Path = SystemType,Converter = {StaticResource EnumToBooleanConverter},ConverterParameter = {x:Static local:SystemTypes.TypeA}}/>
< RadioButton x:Name =TypeBRadioButtonContent =TypeBCanvas.Left =10Canvas.Top =31
IsChecked ={Binding Path = SystemType,Converter = {StaticResource EnumToBooleanConverter},ConverterParameter = {x:Static local:SystemTypes.TypeB}}/>

< / Canvas>


解决方案

您需要将绑定模式设置为TwoWay,然后在转换器实现方法ConvertBack负责将bool转换为SystemTypes,SystemType的定义包括

  set {systemType = value; OnPropertyChanged(()=>SystemType);} 

为了填充属性,价值改变了。

  OnPropertyChanged(()=>SystemType)


可以工作。我不知道你是否设置了DataContext,如果你没有绑定是不行的。为了纠正这一点,在InitializeComponent()之后添加

  this.DataContext = this; 


I think I've followed the examples given in this post but my property is not changing when button are changed. Any suggestions on where I went wrong?

C# code for enum and class

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>

解决方案

You need to set Binding Mode to TwoWay, then in Converter implement method ConvertBack responsible for converting bool to SystemTypes, in settter of SystemType include

set { systemType = value; OnPropertyChanged(() => "SystemType");}

in order to fill property in that its value was changed.

OnPropertyChanged(() => "SystemType")

can work if you implement interface INotifyPropertyChanged. I cannot you whether you set DataContext, if you did not binding is not working. In order to rectify this after InitializeComponent() add

this.DataContext = this;

这篇关于绑定单选按钮到枚举属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆