gpt4 book ai didi

wpf - 将单选按钮组绑定(bind)到 WPF 中的属性

转载 作者:行者123 更新时间:2023-12-03 11:32:14 25 4
gpt4 key购买 nike

假设我有:

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

我可以以某种方式将我的单选按钮直接绑定(bind)到 SelectedRadioButton属性(property)?我真的需要 RadioButton1IsCheckedRadioButton2IsChecked属性仅用于计算选定的单选按钮。

最佳答案

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

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

关于wpf - 将单选按钮组绑定(bind)到 WPF 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com