码迷,mamicode.com
首页 > Windows程序 > 详细

WPF中多个RadioButton绑定到一个属性

时间:2015-04-22 13:32:48      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

如图样:

技术分享

在View中:

<RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter},ConverterParameter={x:Static enum:RadionButtonOptions.One}}" Content="One" DockPanel.Dock="Top"/>
<RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter},ConverterParameter={x:Static enum:RadionButtonOptions.Two}}" Content="Two" DockPanel.Dock="Top"/>
<RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter},ConverterParameter={x:Static enum:RadionButtonOptions.Three}}" Content="Two" DockPanel.Dock="Top"/>

在ViewModel中:

public const string OptionPropertyName = "Option";
private RadionButtonOptions _option;
public RadionButtonOptions Option
{
    get
    {
        return _option;
    }

    set
    {
        if (_option == value)
            return;

        _option = value;
        RaisePropertyChanged(OptionPropertyName);
    }
}

Converter:

[MarkupExtensionReturnType(typeof(EnumToBooleanConverter))]
[ValueConversion(typeof(Enum), typeof(bool))]
public class EnumToBooleanConverter
    : MarkupExtension, IValueConverter
{
    private static EnumToBooleanConverter converter = null;

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (null == converter)
        {
            converter = new EnumToBooleanConverter();
        }

        return converter;
    }

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

枚举类型:

public enum RadionButtonOptions
{
    One,
    Two,
    Three,
}

================================================================================

整理代码之后把转换器的ConvertBack方法体错写成

return Binding.DoNothing;

也是醉了。

WPF中多个RadioButton绑定到一个属性

标签:

原文地址:http://www.cnblogs.com/submarinex/p/4446763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!