zoukankan      html  css  js  c++  java
  • 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}
     }"
  • 相关阅读:
    properties的编码问题
    在Spring中读取properties文件
    Intellij IDEA常用配置记录
    基于Shiro的登录功能 设计思路
    在SpringMVC中操作Session、Request、Response对象
    使用MockMVC与Junit进行单体测试
    django: form fileupload
    django: form
    django: db
    django: db
  • 原文地址:https://www.cnblogs.com/huangsitao/p/10299776.html
Copyright © 2011-2022 走看看