结合单选按钮组在WPF属性 [英] binding radiobuttons group to a property in WPF
本文介绍了结合单选按钮组在WPF属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们想象一下,我有:
<单选按钮组名=组1=器isChecked{绑定路径= RadioButton1IsChecked}/>
<单选按钮组名=组1=器isChecked{绑定路径= RadioButton2IsChecked}/>
然后在我的数据源类我有:
公共BOOL RadioButton1IsChecked {搞定;组; }
公共BOOL RadioButton2IsChecked {搞定;组; }
公共枚举单选按钮{RadioButton1,RadioButton2,无}
公众的RadioButtons SelectedRadioButton
{
得到
{
如果(this.RadioButtonIsChecked)
返回RadioButtons.RadioButton1;
否则,如果(this.RadioButtonIsChecked)
返回RadioButtons.RadioButton2;
其他
返回RadioButtons.None;
}
}
我可以以某种方式直接绑定我的单选按钮 SelectedRadioButton
属性?我真的需要 RadioButton1IsChecked
和 RadioButton2IsChecked
属性只计算选定的单选按钮。
解决方案
<单选按钮组名=组1器isChecked ={绑定路径= SelectedRadioButton,转换器= {StaticResource的EnumBooleanConverter}, ConverterParameter = RadioButton1}/>
<单选按钮组名=组1=器isChecked{绑定路径= SelectedRadioButton,转换器= {StaticResource的EnumBooleanConverter},ConverterParameter = RadioButton2}/>
公共枚举单选按钮{RadioButton1,RadioButton2,无}
公共单选按钮SelectedRadioButton {获取;集;} 公共类EnumBooleanConverter:的IValueConverter
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
{
VAR ParameterString =参数作为字符串;
如果(ParameterString == NULL)
返回DependencyProperty.UnsetValue; 如果(Enum.IsDefined(value.GetType(),价值)== FALSE)
返回DependencyProperty.UnsetValue; 对象paramvalue = Enum.Parse(value.GetType(),ParameterString);
返回paramvalue.Equals(值);
} 公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
{
VAR ParameterString =参数作为字符串;
VAR valueAsBool =(布尔)值; 如果(ParameterString == NULL ||!valueAsBool)
{
尝试
{
返回Enum.Parse(TARGETTYPE,0);
}
赶上(例外)
{
返回DependencyProperty.UnsetValue;
}
}
返回Enum.Parse(TARGETTYPE,ParameterString);
}
}
Let's imagine that I have:
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton1IsChecked}" />
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton2IsChecked}" />
And then in my data source class I have:
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;
}
}
Can I somehow bind my radio buttons directly to SelectedRadioButton
property? I really need RadioButton1IsChecked
and RadioButton2IsChecked
properties only to calculate the selected radiobutton.
解决方案
<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);
}
}
这篇关于结合单选按钮组在WPF属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文