WPF开发快速入门【6】下拉框与枚举类型

张春  金牌会员 | 2022-9-16 17:22:23 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 904|帖子 904|积分 2712

概述
本文讲述下拉框和枚举类型进行绑定的一些操作。
 
下拉框的基本操作
设计部分:
  1.         <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>
复制代码
代码部分:
  1.         <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>public List Fruits { get; set; } = new List { "Apple","Bonana", "Strawberry" };        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>public string SelectedFruit { get; set; }        <ComboBox ItemsSource="{Binding Fruits}"
  16.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  17.         <ComboBox ItemsSource="{Binding Fruits}"
  18.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  19.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  20.         <ComboBox ItemsSource="{Binding Fruits}"
  21.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  22.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>public int SelectedIndex { get; set; }
复制代码
以上是最简单的一种绑定形式,采用字符串列表作为数据源。虽然SelectedItem和SelectedIndex都可以用来控制或获取选择项,但建议还是使用SelectedItem比较合适,因为下拉框的数据是可以绑定任意对象列表的,通过SelectedItem就可以直接获取到对象。注意用于绑定的对象应重写ToString()方法。
 
下拉框绑定枚举类型
首先我们定义一个枚举类型 
  1.     public enum AlarmLevel    {        <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>       Normal = 0,        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>      Warring = 1,        <ComboBox ItemsSource="{Binding Fruits}"
  16.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  17.         <ComboBox ItemsSource="{Binding Fruits}"
  18.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  19.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  20.         <ComboBox ItemsSource="{Binding Fruits}"
  21.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  22.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>      Error = 2,    }
复制代码
  然后定义要绑定的源:
  1.         <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>public List Fruits { get; set; } = new List { AlarmLevel.Normal, AlarmLevel.Warring, AlarmLevel.Error };        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>public AlarmLevel SelectedFruit { get; set; }
复制代码
 设计端代码:
  1.         <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>
复制代码
 这样就实现绑定了。但初始化枚举对象列表的代码有点麻烦,我们需要优化一下。
新建一个辅助类
  1.     public class EnumHelper    {        <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>public static List ToList()        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>{        <ComboBox ItemsSource="{Binding Fruits}"
  16.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  17.         <ComboBox ItemsSource="{Binding Fruits}"
  18.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  19.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  20.         <ComboBox ItemsSource="{Binding Fruits}"
  21.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  22.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    return Enum.GetValues(typeof(T)).Cast().ToList();        <ComboBox ItemsSource="{Binding Fruits}"
  23.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  24.         <ComboBox ItemsSource="{Binding Fruits}"
  25.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  26.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  27.         <ComboBox ItemsSource="{Binding Fruits}"
  28.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  29.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>}    }
复制代码
 初始化代码就可以简化为下面形式:
  1. public List<AlarmLevel> Fruits = EnumHelper<AlarmLevel>.ToList();
复制代码
 这个代码对所有枚举类型都适用。 
 
下拉框中文字显示枚举的描述信息
有时候我们希望下拉框显示的内容和枚举项的定义名称并不一致,比如定义项为“Normal”,界面显示“正常”,这个要求也是可以实现的。下面代码实现界面显示枚举项的Description值。
定义枚举类对象,如下:
  1.     public enum AlarmLevel    {        <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>[Description("正常")]        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>Normal = 0,        <ComboBox ItemsSource="{Binding Fruits}"
  16.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  17.         <ComboBox ItemsSource="{Binding Fruits}"
  18.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  19.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  20.         <ComboBox ItemsSource="{Binding Fruits}"
  21.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  22.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>[Description("警报")]        <ComboBox ItemsSource="{Binding Fruits}"
  23.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  24.         <ComboBox ItemsSource="{Binding Fruits}"
  25.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  26.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  27.         <ComboBox ItemsSource="{Binding Fruits}"
  28.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  29.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>Warring = 1,        <ComboBox ItemsSource="{Binding Fruits}"
  30.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  31.         <ComboBox ItemsSource="{Binding Fruits}"
  32.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  33.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  34.         <ComboBox ItemsSource="{Binding Fruits}"
  35.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  36.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>[Description("故障")]        <ComboBox ItemsSource="{Binding Fruits}"
  37.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  38.         <ComboBox ItemsSource="{Binding Fruits}"
  39.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  40.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  41.         <ComboBox ItemsSource="{Binding Fruits}"
  42.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  43.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>Error = 2,    }
复制代码
 ViewModel中代码不变,我们要增加一个转换器来实现这个功能
  1.     public class EnumDescriptionConverter : IValueConverter    {        <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>{        <ComboBox ItemsSource="{Binding Fruits}"
  16.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  17.         <ComboBox ItemsSource="{Binding Fruits}"
  18.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  19.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  20.         <ComboBox ItemsSource="{Binding Fruits}"
  21.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  22.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    Enum myEnum = (Enum)value;        <ComboBox ItemsSource="{Binding Fruits}"
  23.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  24.         <ComboBox ItemsSource="{Binding Fruits}"
  25.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  26.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  27.         <ComboBox ItemsSource="{Binding Fruits}"
  28.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  29.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    string description = GetEnumDescription(myEnum);        <ComboBox ItemsSource="{Binding Fruits}"
  30.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  31.         <ComboBox ItemsSource="{Binding Fruits}"
  32.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  33.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  34.         <ComboBox ItemsSource="{Binding Fruits}"
  35.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  36.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    return description;        <ComboBox ItemsSource="{Binding Fruits}"
  37.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  38.         <ComboBox ItemsSource="{Binding Fruits}"
  39.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  40.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  41.         <ComboBox ItemsSource="{Binding Fruits}"
  42.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  43.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>}        <ComboBox ItemsSource="{Binding Fruits}"
  44.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  45.         <ComboBox ItemsSource="{Binding Fruits}"
  46.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  47.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  48.         <ComboBox ItemsSource="{Binding Fruits}"
  49.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  50.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)        <ComboBox ItemsSource="{Binding Fruits}"
  51.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  52.         <ComboBox ItemsSource="{Binding Fruits}"
  53.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  54.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  55.         <ComboBox ItemsSource="{Binding Fruits}"
  56.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  57.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>{        <ComboBox ItemsSource="{Binding Fruits}"
  58.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  59.         <ComboBox ItemsSource="{Binding Fruits}"
  60.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  61.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  62.         <ComboBox ItemsSource="{Binding Fruits}"
  63.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  64.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    return string.Empty;        <ComboBox ItemsSource="{Binding Fruits}"
  65.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  66.         <ComboBox ItemsSource="{Binding Fruits}"
  67.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  68.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  69.         <ComboBox ItemsSource="{Binding Fruits}"
  70.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  71.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>}        <ComboBox ItemsSource="{Binding Fruits}"
  72.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  73.         <ComboBox ItemsSource="{Binding Fruits}"
  74.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  75.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  76.         <ComboBox ItemsSource="{Binding Fruits}"
  77.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  78.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>private string GetEnumDescription(Enum enumObj)        <ComboBox ItemsSource="{Binding Fruits}"
  79.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  80.         <ComboBox ItemsSource="{Binding Fruits}"
  81.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  82.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  83.         <ComboBox ItemsSource="{Binding Fruits}"
  84.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  85.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>{        <ComboBox ItemsSource="{Binding Fruits}"
  86.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  87.         <ComboBox ItemsSource="{Binding Fruits}"
  88.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  89.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  90.         <ComboBox ItemsSource="{Binding Fruits}"
  91.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  92.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());        <ComboBox ItemsSource="{Binding Fruits}"
  93.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  94.         <ComboBox ItemsSource="{Binding Fruits}"
  95.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  96.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  97.         <ComboBox ItemsSource="{Binding Fruits}"
  98.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  99.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    object[] attribArray = fieldInfo.GetCustomAttributes(false);        <ComboBox ItemsSource="{Binding Fruits}"
  100.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  101.         <ComboBox ItemsSource="{Binding Fruits}"
  102.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  103.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  104.         <ComboBox ItemsSource="{Binding Fruits}"
  105.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  106.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    if (attribArray.Length == 0)        <ComboBox ItemsSource="{Binding Fruits}"
  107.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  108.         <ComboBox ItemsSource="{Binding Fruits}"
  109.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  110.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  111.         <ComboBox ItemsSource="{Binding Fruits}"
  112.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  113.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    {        <ComboBox ItemsSource="{Binding Fruits}"
  114.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  115.         <ComboBox ItemsSource="{Binding Fruits}"
  116.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  117.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  118.         <ComboBox ItemsSource="{Binding Fruits}"
  119.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  120.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  121.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  122.         <ComboBox ItemsSource="{Binding Fruits}"
  123.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  124.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  125.         <ComboBox ItemsSource="{Binding Fruits}"
  126.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  127.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>return enumObj.ToString();        <ComboBox ItemsSource="{Binding Fruits}"
  128.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  129.         <ComboBox ItemsSource="{Binding Fruits}"
  130.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  131.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  132.         <ComboBox ItemsSource="{Binding Fruits}"
  133.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  134.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    }        <ComboBox ItemsSource="{Binding Fruits}"
  135.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  136.         <ComboBox ItemsSource="{Binding Fruits}"
  137.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  138.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  139.         <ComboBox ItemsSource="{Binding Fruits}"
  140.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  141.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    else        <ComboBox ItemsSource="{Binding Fruits}"
  142.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  143.         <ComboBox ItemsSource="{Binding Fruits}"
  144.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  145.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  146.         <ComboBox ItemsSource="{Binding Fruits}"
  147.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  148.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    {        <ComboBox ItemsSource="{Binding Fruits}"
  149.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  150.         <ComboBox ItemsSource="{Binding Fruits}"
  151.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  152.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  153.         <ComboBox ItemsSource="{Binding Fruits}"
  154.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  155.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  156.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  157.         <ComboBox ItemsSource="{Binding Fruits}"
  158.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  159.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  160.         <ComboBox ItemsSource="{Binding Fruits}"
  161.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  162.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;        <ComboBox ItemsSource="{Binding Fruits}"
  163.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  164.         <ComboBox ItemsSource="{Binding Fruits}"
  165.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  166.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  167.         <ComboBox ItemsSource="{Binding Fruits}"
  168.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  169.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  170.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  171.         <ComboBox ItemsSource="{Binding Fruits}"
  172.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  173.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  174.         <ComboBox ItemsSource="{Binding Fruits}"
  175.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  176.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>return attrib.Description;        <ComboBox ItemsSource="{Binding Fruits}"
  177.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  178.         <ComboBox ItemsSource="{Binding Fruits}"
  179.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  180.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  181.         <ComboBox ItemsSource="{Binding Fruits}"
  182.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  183.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>    }        <ComboBox ItemsSource="{Binding Fruits}"
  184.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  185.         <ComboBox ItemsSource="{Binding Fruits}"
  186.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  187.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  188.         <ComboBox ItemsSource="{Binding Fruits}"
  189.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  190.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>}    }
复制代码
View Code 前端代码修改如下:
  1.         <ComboBox ItemsSource="{Binding Fruits}"
  2.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  3.         <ComboBox ItemsSource="{Binding Fruits}"
  4.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  5.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  6.         <ComboBox ItemsSource="{Binding Fruits}"
  7.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  8.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  9.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  10.         <ComboBox ItemsSource="{Binding Fruits}"
  11.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  12.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  13.         <ComboBox ItemsSource="{Binding Fruits}"
  14.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  15.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  16.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  17.         <ComboBox ItemsSource="{Binding Fruits}"
  18.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  19.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  20.         <ComboBox ItemsSource="{Binding Fruits}"
  21.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  22.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  23.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  24.         <ComboBox ItemsSource="{Binding Fruits}"
  25.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  26.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  27.         <ComboBox ItemsSource="{Binding Fruits}"
  28.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  29.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  30.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  31.         <ComboBox ItemsSource="{Binding Fruits}"
  32.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  33.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  34.         <ComboBox ItemsSource="{Binding Fruits}"
  35.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  36.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  37.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  38.         <ComboBox ItemsSource="{Binding Fruits}"
  39.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  40.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  41.         <ComboBox ItemsSource="{Binding Fruits}"
  42.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  43.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  44.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  45.         <ComboBox ItemsSource="{Binding Fruits}"
  46.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  47.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  48.         <ComboBox ItemsSource="{Binding Fruits}"
  49.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  50.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  51.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  52.         <ComboBox ItemsSource="{Binding Fruits}"
  53.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  54.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  55.         <ComboBox ItemsSource="{Binding Fruits}"
  56.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  57.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  58.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  59.         <ComboBox ItemsSource="{Binding Fruits}"
  60.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  61.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  62.         <ComboBox ItemsSource="{Binding Fruits}"
  63.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  64.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  65.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  66.         <ComboBox ItemsSource="{Binding Fruits}"
  67.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  68.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  69.         <ComboBox ItemsSource="{Binding Fruits}"
  70.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  71.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  72.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  73.         <ComboBox ItemsSource="{Binding Fruits}"
  74.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  75.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  76.         <ComboBox ItemsSource="{Binding Fruits}"
  77.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  78.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  79.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  80.         <ComboBox ItemsSource="{Binding Fruits}"
  81.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  82.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  83.         <ComboBox ItemsSource="{Binding Fruits}"
  84.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  85.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  86.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  87.         <ComboBox ItemsSource="{Binding Fruits}"
  88.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  89.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  90.         <ComboBox ItemsSource="{Binding Fruits}"
  91.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  92.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  93.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  94.         <ComboBox ItemsSource="{Binding Fruits}"
  95.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  96.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  97.         <ComboBox ItemsSource="{Binding Fruits}"
  98.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  99.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  100.                   SelectedItem="{Binding SelectedFruit}"/><ComboBox ItemsSource="{Binding Fruits}"
  101.         <ComboBox ItemsSource="{Binding Fruits}"
  102.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  103.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedItem="{Binding SelectedFruit}"
  104.         <ComboBox ItemsSource="{Binding Fruits}"
  105.                   SelectedItem="{Binding SelectedFruit}"/>        <ComboBox ItemsSource="{Binding Fruits}"
  106.                   SelectedItem="{Binding SelectedFruit}"/>  SelectedIndex="{Binding SelectedIndex}"/>      
复制代码
 这里稍微分析一下转换器。
这里的转换器必须继承于IValueConverter接口,它有Convert和ConvertBack两个方法。以Convert为例,其输入是一个object,来源是ItemTemplate模板的当前对象,输出也是一个对象,ItemTemplate会用输出的对象代替原来的对象。
自定义的输出的对象应重写ToString()方法。
以上转换器的设计是将枚举对象转换为其Description特性,实际应用时可能需要考虑国际化的需求,其转换数据的来源也不一定要从Description取值,完全可以根据需要另行设计。
 
资源
系列目录:WPF开发快速入门【0】前言与目录 
代码下载:Learn WPF: WPF学习笔记 (gitee.com)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

张春

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表