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}"/> 

  • 相关阅读:
    Cookie和Session知识扫盲
    Nmap扫描原理与用法
    物理cpu与逻辑cpu的理解
    shell常用命令ping
    shell如何获取本地ip
    数据库52条SQL语句性能优化
    Linux Shell查看物理CPU个数、核数、逻辑CPU个数
    cf1225D Power Products cf1471D. Strange Definition
    cf 1389 E. Calendar Ambiguity
    cf 1420 D. Rescue Nibel!
  • 原文地址:https://www.cnblogs.com/syqun/p/enum.html
Copyright © 2011-2022 走看看