WPF一组Radio与enum绑定

工作中用到了一组RadioButton对应一组数据的情况,这个时候最好能将一组RadioButton绑定Enum。

步骤

1.MyControl库中Type.cs中定义Enum

1 namespace MyControl.MyRadioButton 
2 {
3      public enum Type { A, B, C, } 
4 }

2.MyUseControl的ViewModel文件夹下testViewModel.cs中

1 private Type m_myType; 
2 public Type MyType 
3 { 
4   get{ return m_myType;} 
5   set
6   { 
7     if(m_myType != value) { m_myType = value; } 
8   } 
9 }

3.MyUserControl的Converses文件夹下Enum2BoolConverter.cs中

 1 class Enum2BoolConverter : IValueConverter
 2 {
 3     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 4     {
 5         return value == null ? false : value.Equals(parameter);
 6     }
 7 
 8     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 9     {
10         return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
11     }
12 }

4.test.xaml文件中需要引用MyControl

1 xmlns:locals="clr-namespace:MyControl.MyRadioButton;assembly=MyControl"
2 <!--引用Converses命名空间-->
3 xmlns:converter="clr-namespace:MyUseControl.Converters"
4 <UserControl.Resources>
5 <converter:Enum2BoolConverter x:Key="enum2BoolConverter" />
6 </UserControl.Resources>

5.test.xaml文件中RadioButton的IsChecked属性中

IsChecked="{Binding MyType,
            Converter={StaticResource enum2BoolConverter},
         ConverterParameter={x:Static locals:Type.A}
 }"

 

转载于:https://www.cnblogs.com/huangsitao/p/10299776.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值