Silverlight将Combobox数据绑定到枚举

时间:2011-03-01 19:36:26

标签: c# mvvm silverlight-4.0

我遇到了一个问题,即我的模型中绑定到枚举的几个组合无法正常工作。首先让我告诉你,我的应用程序中有2个区域可以操作枚举。为了清晰/简单,我们可以说左侧和右侧都有相同的视图,中心有结果/摘要。当我在其中一个视图中设置值时,它会根据设计通过ViewModel将值设置为模型,但另一侧的视图中的组合(再次仅为了清晰/简单起见)似乎没有更新。它应该选择适当的行/项,但不是。以下是我目前正在使用的代码片段:

//Definition of Enum:
public enum eNumericAndDateOperator 
{ GreaterThan, GreaterThanOrEqualTo, Equals, LessThanOrEqualTo, LessThan, Between, Ignore }


//XAML of Combo in View(s):
<ComboBox>
   <ComboBoxItem Content="" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Ignore}" />
   <ComboBoxItem Content="&lt;" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=LessThan}" />
   <ComboBoxItem Content="&lt;=" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=LessThanOrEqualTo}" />
   <ComboBoxItem Content="=" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Equals}" />
   <ComboBoxItem Content="&gt;=" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=GreaterThan}" />
   <ComboBoxItem Content="&gt;" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=GreaterThanOrEqualTo}" />
   <ComboBoxItem Content="Between" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Between}" />
</ComboBox>

   //Enum Converter code
   public class EnumToBoolConverter : IValueConverter
   {
      #region Methods
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (value == null || parameter == null)
            return value;
         return value.ToString() == parameter.ToString();
      }
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (value == null || parameter == null)
            return value;

         return Enum.Parse(targetType, parameter.ToString(), true);
      }
      #endregion Methods
   }

EnumConverter是你的基本Enum to Bool转换器类(并且定义了ConvertBack是的。)

我应该在组合中使用选定的索引吗?我无法通过EnumToIEnumerableConverter或其他东西将枚举作为组合的ItemSource加载,因为我想为每个项目自定义可见文本...

1 个答案:

答案 0 :(得分:1)

为什么不将枚举转换为具有静态属性的简单类

public class Operator
{
     public string Key { get; set; }
     public string Caption { get; set; }
     ...

    public static Operator GreaterThan { get { ... } }
    public static Operator LessThan { get { ... } }  

    public static IList<Operator> Operators { get { ... } }
}

在ViewModel中添加一个引用静态Operator.Operators属性的Operators属性

在您看来:

<ComboBox ItemsSource="{Binding Operators}"
      SelectedItem="{Binding Path=SelectedOperator, Mode=TwoWay}" />

然后为显示Caption的Operator类创建一个DataTemplate。这种方法的优点是你可以轻松地为你的操作符类添加新的功能 - 使用枚举你将永远是有限的