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

    今日Google这个问题,呀,还真的在国外发现许多解决方法,其中经典的有:

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

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

  • 相关阅读:
    JavaScript深拷贝和浅拷贝
    JavaScript正则表达式(一)-常用方法
    面向对象之继承-5种JavaScript继承的方法
    java学习笔记之System类
    MySQL的grant,revoke使用
    LoadRunner+Android模所器录制脚本
    Mysql
    mosquitto_pub和mosquitto_sub 命令参数说明
    【转载】原 IntelliJ IDEA (idea)引入eclipse web项目
    eclipse恢复默认布局
  • 原文地址:https://www.cnblogs.com/sofire/p/1690111.html
Copyright © 2011-2022 走看看