WPF Radiobutton(二)(绑定到布尔值) [英] WPF Radiobutton (two) (binding to boolean value)

查看:90
本文介绍了WPF Radiobutton(二)(绑定到布尔值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复选框的布尔类型的属性.

I have a property of type boolean presented with checkbox.

我想将其更改为绑定在同一属性上的两个单选按钮,呈现值 true/false.

I want to change that to two radiobuttons that bind on the same property presenting the value true/false.

这怎么办?

推荐答案

<RadioButton GroupName="Group1" 
             IsChecked="{Binding PropertyValue}" Content="Yes" />
<RadioButton GroupName="Group1"  Content="No" 
             IsChecked="{Binding PropertyValue, 
                         Converter={StaticResource BoolInverterConverter}}" />

public class BoolInverterConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            return !(bool)value;
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            return !(bool)value;
        }
        return value;
    }

    #endregion
}

这篇关于WPF Radiobutton(二)(绑定到布尔值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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