【问题标题】:binding to a radio button绑定到单选按钮
【发布时间】:2011-10-06 10:25:45
【问题描述】:

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

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

如何让 No 按钮仅使用 XAML 来获取相反的值?

【问题讨论】:

    标签: wpf radio-button bind


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      没有仅适用于 Xaml 的解决方案。不过,您可以使用反向布尔转换器绑定 No。

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

      【讨论】:

      • 我如何将它绑定到 No 按钮?
      • {Binding IsAllowed, Mode=TwoWay, Converter=notConverter}
      【解决方案3】:

      你不需要。默认情况下会发生。

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

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

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

      【讨论】:

        【解决方案4】:

        您必须使用IValueConverter 编写转换器。这是一个例子如何做到这一点WPF - Bind to Opposite Boolean Value Using a Converter

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-29
          • 2021-07-19
          • 2011-01-20
          • 2012-02-12
          • 2015-10-31
          • 1970-01-01
          • 2011-02-07
          • 2017-02-12
          相关资源
          最近更新 更多