zoukankan      html  css  js  c++  java
  • 在WPF中如何将Enum 绑定到 集合控件

    第一种,通过绑定转换器:

    public sealed class EnumToNamesConverter : IValueConverter  {    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)    {      return Enum.GetNames(value.GetType());    }      object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)    {      throw New NotSupportedException()    }  } 

     

    XAML

    <local:EnumToNamesConverter x:Key="EnumToNamesConverter" />

     

    <ComboBox ItemsSource="{Binding                          Source={x:Type local:CompassHeading},                          Converter={StaticResource EnumToNamesConverter}}" /> 

    第二种,经典呀!通过继承MarkupExtension

     

    [MarkupExtensionReturnType(typeof(object[]))]  public class EnumValuesExtension : MarkupExtension  {      public EnumValuesExtension()      {      }        public EnumValuesExtension(Type enumType)      {          this.EnumType = enumType;      }        [ConstructorArgument("enumType")]      public Type EnumType { get; set; }        public override object ProvideValue(IServiceProvider serviceProvider)      {          if (this.EnumType == null)              throw new ArgumentException("The enum type is not set");          return Enum.GetValues(this.EnumType);      }  } 

    XAML

    <ComboBox ItemsSource="{local:EnumValues local:EmployeeType}"/> 

  • 相关阅读:
    C++输入与输出
    数组与指针
    MFC+WinPcap编写一个嗅探器之零(目录)
    netty源码分析之揭开reactor线程的面纱(二)
    netty源码分析之揭开reactor线程的面纱(一)
    Vert.x 线程模型揭秘
    理解 RxJava 的线程模型
    Java RESTful 框架的性能比较
    Java借助CountDownLatch完成异步回调
    在 Java 中运用动态挂载实现 Bug 的热修复
  • 原文地址:https://www.cnblogs.com/syqun/p/enum.html
Copyright © 2011-2022 走看看