wpf 将 Xaml 单选按钮绑定到布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18449890/
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 09:30:25  来源:igfitidea点击:

Bind Xaml Radio button to boolean

wpfxaml

提问by ashish g

I saw many posts on how to bind a boolean value to a radio button. But my scenario is that I need to bind it to a radio button and read the selection from the user.

我看到很多关于如何将布尔值绑定到单选按钮的帖子。但我的情况是我需要将它绑定到一个单选按钮并读取用户的选择。

That is no option should be selected intially.

即最初不应选择任何选项。

If I bind it to a boolean, since boolean cant be null it shows the default value selected in radio button.

如果我将它绑定到一个布尔值,因为布尔值不能为空,它会显示在单选按钮中选择的默认值。

If I use nullable boolean, I still defaults to false when trying to use the converter.

如果我使用可为空的布尔值,在尝试使用转换器时我仍然默认为 false。

I cant use oneway mode in xaml as I need to check if the radio button selection was made which I do using the bounded variable.

我无法在 xaml 中使用单向模式,因为我需要检查是否使用有界变量进行了单选按钮选择。

Ant pointers on how to achieve this?

这个ant指点如何实现?

回答by ChrisF

You can bind a nullable boolean to your radio buttons, but you need to do it through a converter.

您可以将可为空的布尔值绑定到您的单选按钮,但您需要通过转换器来完成。

First declare your variable:

首先声明你的变量:

private bool? answer;
public bool? Answer
{
    get { return answer; }
    set
    {
        answer = value;
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Answer"));
    }
}

and initialise it to null.

并将其初始化为null.

Then in the XAML:

然后在 XAML 中:

<Window.Resources>
    <local:BooleanConverter x:Key="BooleanConverter"/>
</Window.Resources>

<StackPanel Grid.Row="1">
    <RadioButton Content="I Agree"
                 IsChecked="{Binding Answer,
                             Converter={StaticResource BooleanConverter},
                             ConverterParameter='true', Mode=TwoWay}" />
    <RadioButton Content="I Disagree"
                 IsChecked="{Binding Answer,
                             Converter={StaticResource BooleanConverter},
                             ConverterParameter='false', Mode=TwoWay}" />
</StackPanel>

And finally your converter:

最后你的转换器:

class BooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var test = (bool?)value;
        var result = bool.Parse((string)parameter);

        if (test == result)
        {
            return true;
        }

        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var result = bool.Parse((string)parameter);
        return result;
    }
}

This binds the radio button to Answerand states that the first one will only be checked when the value is trueand the second one will be checked when the answer is false. You can then check that Answeris not null before letting the user proceed.

这将单选按钮绑定到Answer并声明第一个将仅在值为 时检查true,而第二个将在答案​​为 时检查false。然后,您可以Answer在让用户继续之前检查它是否为空。

回答by BaconSah

This may not be the best way of doing it, but you could use something similar to this: checkbox like radiobutton wpf c#and just have two controls.

这可能不是最好的方法,但您可以使用类似于以下内容的方法: 复选框,例如单选按钮 wpf c#并且只有两个控件。

I appreciate it's not the best, but reading your answer it gives you what you want (generally).

我很欣赏它不是最好的,但阅读你的答案它会给你你想要的(通常)。