绑定到单选按钮

发布于 2024-11-24 09:04:32 字数 234 浏览 0 评论 0原文

我将以下单选按钮绑定到变量 IsAllowed

<RadioButton Name="YesRadioButton" Margin="5,5,0,0" IsChecked="{Binding Path=IsAllowed, Mode=TwoWay}">Yes</RadioButton>

如何使“否”按钮仅使用 XAML 获取相反的值?

I have the following radio button bound to the variable IsAllowed

<RadioButton Name="YesRadioButton" Margin="5,5,0,0" IsChecked="{Binding Path=IsAllowed, Mode=TwoWay}">Yes</RadioButton>

How can I make the No button to take the opposite value only using XAML ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜雨飘雪 2024-12-01 09:04:33

您必须使用 IValueConverter 编写转换器。以下是如何执行此操作的示例WPF - 使用转换器绑定到相反的布尔值

You will have to write converter using IValueConverter. Here is an example how to do it WPF - Bind to Opposite Boolean Value Using a Converter

北方的韩爷 2024-12-01 09:04:33

上面的答案有效,但我想要一个适用于“是”和“否”单选按钮并反映可为空布尔值的转换器。因此,我采用了利用转换器参数的替代方案:

public class YesNoRadioButtonToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return CompareValueWithRequiredValueToBeChecked(value, parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return CompareValueWithRequiredValueToBeChecked(value, parameter);
    }

    private bool CompareValueWithRequiredValueToBeChecked(object value, object parameterValue)
    {
        bool? convertedValue = ConvertObjectToBool(value);
        bool? convertedParameter = ConvertObjectToBool(parameterValue);

        bool result = convertedValue == convertedParameter;

        return result;
    }

    private bool? ConvertObjectToBool(object parameter)
    {
        string stringResult = parameter == null ? null : parameter.ToString();
        bool? convertedResult;

        bool convertResultTest = false;
        if (stringResult != null && !bool.TryParse(stringResult, out convertResultTest))
        {
            throw new InvalidCastException(string.Format("Cannot convert {0} to a bool.", parameter));
        }

        convertedResult = stringResult == null ? (bool?)null : (bool?)convertResultTest;

        return convertedResult;
    }
}

XAML 如下所示:

    <converters:YesNoRadioButtonToBooleanConverter x:Key="yesNoToBool" />

    <RadioButton Content="Yes" Name="radYes" GroupName="Group1" IsChecked="{Binding Path=boolProperty1, Mode=TwoWay, Converter={StaticResource yesNoToBool}, ConverterParameter=true}" />
    <RadioButton Content="No" Name="radNo" GroupName="Group1" IsChecked="{Binding Path=boolProperty1, Mode=TwoWay, Converter={StaticResource yesNoToBool}, ConverterParameter=false}" />

The above answer works but I wanted a converter that would apply to both Yes and No radio buttons and reflect the value of nullable booleans. So I made the alternative that takes advantage of converter parameters:

public class YesNoRadioButtonToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return CompareValueWithRequiredValueToBeChecked(value, parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return CompareValueWithRequiredValueToBeChecked(value, parameter);
    }

    private bool CompareValueWithRequiredValueToBeChecked(object value, object parameterValue)
    {
        bool? convertedValue = ConvertObjectToBool(value);
        bool? convertedParameter = ConvertObjectToBool(parameterValue);

        bool result = convertedValue == convertedParameter;

        return result;
    }

    private bool? ConvertObjectToBool(object parameter)
    {
        string stringResult = parameter == null ? null : parameter.ToString();
        bool? convertedResult;

        bool convertResultTest = false;
        if (stringResult != null && !bool.TryParse(stringResult, out convertResultTest))
        {
            throw new InvalidCastException(string.Format("Cannot convert {0} to a bool.", parameter));
        }

        convertedResult = stringResult == null ? (bool?)null : (bool?)convertResultTest;

        return convertedResult;
    }
}

Here is what the XAML looks like:

    <converters:YesNoRadioButtonToBooleanConverter x:Key="yesNoToBool" />

    <RadioButton Content="Yes" Name="radYes" GroupName="Group1" IsChecked="{Binding Path=boolProperty1, Mode=TwoWay, Converter={StaticResource yesNoToBool}, ConverterParameter=true}" />
    <RadioButton Content="No" Name="radNo" GroupName="Group1" IsChecked="{Binding Path=boolProperty1, Mode=TwoWay, Converter={StaticResource yesNoToBool}, ConverterParameter=false}" />
趴在窗边数星星i 2024-12-01 09:04:32

你不需要这样做。默认情况下会发生这种情况。

只需确保 IsAllowedtrue 开头,其余部分将自行处理。

这是因为当您单击No按钮时,它会自动设置Yes按钮的选中值(这就是单选按钮的工作原理),因此更改将自动发生,并且您支持类将被更新。

更好:只需使用复选框即可。是/否的情况正是它们的设计目的。

You do not need to. It will happen by default.

Just make sure that IsAllowed starts off as true, and the rest will take care of its self.

This is because when you click on the No button, it will automatically set the Yes button's checked value (that's how radio buttons work), so the change will happen automatically and you backing class will be updated.

EVEN BETTER: Just use a check box. Yes/no situations are what they are designed for.

最初的梦 2024-12-01 09:04:32

不存在仅 Xaml 的解决方案。不过,您可以使用反向布尔转换器绑定“否”。

<local:NotConverter x:Key="notConverter"/>

{Binding IsAllowed, Mode=TwoWay, Converter=notConverter} 

public class NotConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Boolean result = false;
        if (value is Boolean)
            result = !((Boolean)value);
        return result;
    }

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

There is no Xaml-only solution. You could bind No using a reverse bool Converter though.

<local:NotConverter x:Key="notConverter"/>

{Binding IsAllowed, Mode=TwoWay, Converter=notConverter} 

public class NotConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Boolean result = false;
        if (value is Boolean)
            result = !((Boolean)value);
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Boolean result = false;
        if (value is Boolean)
            result = !((Boolean)value);
        return result;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文