WPF Radiobutton(二)(绑定到布尔值)

no9*_*no9 39 wpf binding boolean radio-button

我有一个类型为boolean的属性,带有复选框.

我想将其更改为绑定在同一属性上的两个radiobutton,其值为true/false.

怎么办?

Rag*_*han 75

<RadioButton GroupName="Group1" 
             IsChecked="{Binding PropertyValue}" Content="Yes" />
<RadioButton GroupName="Group1"  Content="No" 
             IsChecked="{Binding PropertyValue, 
                         Converter={StaticResource BoolInverterConverter}}" />
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)

  • 出于某种原因,我必须使用两个不同的GroupNames.否则,ViewModel中的布尔值可以设置为true,但永远不会重置为false(即使单选按钮看起来行为正常) (25认同)

Ran*_*ngy 11

无论何时加载UI,标准绑定方法都会将绑定设置器触发为"未选定",这是一种令人遗憾的副作用.因此,如果你有代码来处理用户在bool的setter中的点击次数,它会做一些奇怪的事情,比如将setter点击为"false",即使你已将它绑定到"true"bool.

我使用专门用于单选按钮的转换器来解决这个问题:

public class BoolRadioConverter : IValueConverter
{
    public bool Inverse { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool boolValue = (bool) value;

        return this.Inverse ? !boolValue : boolValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool boolValue = (bool)value;

        if (!boolValue)
        {
            // We only care when the user clicks a radio button to select it.
            return null;
        }

        return !this.Inverse;
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的资源中:

<converters:BoolRadioConverter x:Key="BoolRadioConverter" />
<converters:BoolRadioConverter x:Key="InverseBoolRadioConverter" Inverse="True" />
Run Code Online (Sandbox Code Playgroud)

在你的xaml中:

<RadioButton
    Content="True option"
    GroupName="radioGroup1"
    IsChecked="{Binding MyProperty,
                        Converter={StaticResource BoolRadioConverter}}" />
<RadioButton
    Content="False option"
    GroupName="radioGroup2"
    IsChecked="{Binding MyProperty,
                        Converter={StaticResource InverseBoolRadioConverter}}" />
Run Code Online (Sandbox Code Playgroud)


HCL*_*HCL 9

您可以使用值转换器来恢复布尔值:

使用该转换器,将一个Checkbox.IsChecked属性绑定到没有转换器的布尔值和一个带转换器的CheckBox.IsChecked属性.这应该可以解决问题.

这里是这种转换器的代码.我从这里复制了它并添加了一些代码行.在那里你会找到更多的信息.

public class BoolToOppositeBoolConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture) {
        if (targetType != typeof(bool)) {
            throw new InvalidOperationException("The target must be a boolean");
        }
        if (null == value) {
            return null;
        }
                    return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture) {
        if (targetType != typeof(bool)) {
            throw new InvalidOperationException("The target must be a boolean");
        }
        if (null == value) {
            return null;
        }
        return !(bool)value;
    }
} 
Run Code Online (Sandbox Code Playgroud)

要使用它,请在资源部分中声明它.

 <local:BoolToOppositeBoolConverter x:Key="BoolToOppositeBoolConverter_ValueConverter"/>
Run Code Online (Sandbox Code Playgroud)

并将其作为静态资源用于绑定:

<CheckBox IsChecked="{Binding YourProperty}" />
<CheckBox IsChecked="{Binding YourProperty,Converter={StaticResource BoolToOppositeBoolConverter_ValueConverter}}" />
Run Code Online (Sandbox Code Playgroud)

请注意,转换器只是一个简单的例子.如果要在生产代码中使用它,请巧妙地实现它.我没有测试过.如果它不起作用,请发表评论.


vot*_*bac 5

如果将两个单选按钮的 GroupName 属性设置为相同的值(因此一次只能选中一个),则无需转换器即可实现此目的。然后,将一个单选按钮的 IsChecked 设置为“True”,并将另一个单选按钮的 IsChecked 绑定到您的布尔值。切换单选按钮将更改布尔值,但是,将布尔值更改为 False 将不会检查其他单选按钮。

谢谢,弗拉德