【问题标题】:binding radiobuttons group to a property in WPF将单选按钮组绑定到 WPF 中的属性
【发布时间】:2012-03-02 01:01:00
【问题描述】:

假设我有:

<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton1IsChecked}" />
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton2IsChecked}" />

然后在我的数据源类中我有:

public bool RadioButton1IsChecked { get; set; }
public bool RadioButton2IsChecked { get; set; }
public enum RadioButtons { RadioButton1, RadioButton2, None }
public RadioButtons SelectedRadioButton
{
    get
    {
        if (this.RadioButtonIsChecked) 
            return RadioButtons.RadioButton1;
        else if (this.RadioButtonIsChecked) 
            return RadioButtons.RadioButton2;
        else 
            return RadioButtons.None;
     }
}

我可以以某种方式将我的单选按钮直接绑定到SelectedRadioButton 属性吗?我真的只需要 RadioButton1IsCheckedRadioButton2IsChecked 属性来计算选定的单选按钮。

【问题讨论】:

标签: wpf binding radio-button radio-group


【解决方案1】:

声明一个类似于以下的枚举:

enum RadioOptions {Option1, Option2}

XAML:

<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={x:Static local:RadioOptions.Option1}}"/>
<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={x:Static local:RadioOptions.Option2}}"/>

转换器类:

public class EnumBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value) ? parameter : Binding.DoNothing;
    }
}

【讨论】:

  • 与接受的答案有什么功能区别?
  • @Jerther:接受的答案采用字符串参数并将其转换为枚举。这个直接接受一个枚举值,这是一个更好的主意,因为如果参数不正确,它将无法编译。
【解决方案2】:
<RadioButton GroupName="Group1" IsChecked="{Binding Path=SelectedRadioButton, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=RadioButton1}" />
<RadioButton GroupName="Group1" IsChecked="{Binding Path=SelectedRadioButton, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=RadioButton2}" />
public enum RadioButtons { RadioButton1, RadioButton2, None }
public RadioButtons SelectedRadioButton {get;set;}

 public class EnumBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false)
                return DependencyProperty.UnsetValue;

            object paramvalue = Enum.Parse(value.GetType(), ParameterString);
            return paramvalue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            var valueAsBool = (bool) value;

            if (ParameterString == null || !valueAsBool)
            {
                try
                {
                    return Enum.Parse(targetType, "0");
                }
                catch (Exception)
                {
                    return DependencyProperty.UnsetValue;
                }
            }
            return Enum.Parse(targetType, ParameterString);
        }
    }

【讨论】:

  • 当任何单选按钮的IsChecked 属性设置为false 时,这不会将SelectedRadioButton 值设置为RadioButtons.None(通过执行return Enum.Parse(targetType, "0"); )吗?
【解决方案3】:

我们可以动态创建单选按钮,ListBox 可以帮助我们做到这一点,无需转换器,非常简单。

优势如下: 如果有一天你的枚举类发生了变化,你不需要更新 GUI(XAML 文件)。

步骤如下: 创建一个 ListBox 并将列表框的 ItemsSource 设置为枚举,并将 ListBox 的 SelectedItem 绑定到 Selected 属性。 然后将创建每个 ListBoxItem 的单选按钮。

public enum RadioButtons
{ 
   RadioButton1, 
   RadioButton2, 
   None
}
  • 第 1 步:将枚举添加到 Window、UserControl 或 Grid 等的静态资源中。
    <Window.Resources>
        <ObjectDataProvider MethodName="GetValues"
                            ObjectType="{x:Type system:Enum}"
                            x:Key="RadioButtons">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:RadioButtons" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
  • 第 2 步:使用列表框和 Control Template 将其中的每个项目填充为单选按钮
    <ListBox ItemsSource="{Binding Source={StaticResource RadioButtons}}" SelectedItem="{Binding SelectedRadioButton, Mode=TwoWay}" >
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <RadioButton
                                Content="{TemplateBinding ContentPresenter.Content}"
                                IsChecked="{Binding Path=IsSelected,
                                RelativeSource={RelativeSource TemplatedParent},
                                Mode=TwoWay}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.Resources>
    </ListBox>

现在,享受吧~

参考资料: https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/

【讨论】:

    【解决方案4】:

    XAML:

    <RadioButton IsChecked="{Binding Path=SelectedOption, UpdateSourceTrigger=PropertyChanged}">Option1</RadioButton>
    <RadioButton IsChecked="{Binding Path=SelectedOption, UpdateSourceTrigger=PropertyChanged, Converter={v:NotBoolenConverter}}">Option2</RadioButton>
    

    转换器:

    public class NotBoolenConverter : IValueConverter
        {
            public NotBoolenConverter()
            {
            }
    
            public override object Convert(
                object value,
                Type targetType,
                object parameter,
                CultureInfo culture)
            {
                bool output = (bool)value;
                return !output;
            }
    
            public override object ConvertBack(
                object value,
                Type targetType,
                object parameter,
                CultureInfo culture)
            {
                bool output = (bool)value;
                return !output;
            }
        }
    

    使用 2 个单选按钮,将一个绑定到另一个的反面。

    【讨论】:

    • 与其他答案相比,这看起来很不令人满意
    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 2010-11-27
    • 2017-12-07
    • 2014-10-14
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多