wpf 将单选按钮绑定到枚举属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25414802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:22:51  来源:igfitidea点击:

Binding radio button to enum property

c#wpfxamlenumsradio-button

提问by James R

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

我想我已经遵循了这篇文章中给出的例子,但是当按钮改变时我的属性没有改变。关于我哪里出错的任何建议?

C# code for enum and class

枚举和类的 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

xml

        <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>

采纳答案by Maximus

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

您需要将Binding Mode设置为TwoWay,然后在Converter实现方法ConvertBack负责将bool转换为SystemTypes,在SystemType的setter中包括

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

如果您实现接口 INotifyPropertyChanged,则可以工作。我不知道你是否设置了DataContext,如果你没有绑定是不工作的。为了在 InitializeComponent() 添加后纠正此问题

this.DataContext = this;