开发者

binding to a radio button

开发者 https://www.devze.com 2023-03-20 11:11 出处:网络
I have the 开发者_StackOverflowfollowing radio button bound to the variable IsAllowed <RadioButton Name=\"YesRadioButton\" Margin=\"5,5,0,0\" IsChecked=\"{Binding Path=IsAllowed, Mode=TwoWay}\">

I have the 开发者_StackOverflowfollowing 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 ?


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.


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


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


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}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消