C# Silverlight将组合框数据绑定到枚举

C# Silverlight将组合框数据绑定到枚举,c#,mvvm,silverlight-4.0,C#,Mvvm,Silverlight 4.0,我遇到了一个问题,模型中绑定到枚举的几个组合无法正常工作。首先,让我告诉你,我的应用程序有两个区域可以操作枚举。为清晰/简单起见,我们只需说,左侧和右侧有相同的视图,中间有结果/摘要。当我在其中一个视图中设置值时,它通过ViewModel将值设置为模型(如设计所示),但是视图中另一侧的组合(同样是为了清晰/简单起见)似乎没有更新。它应该选择了适当的行/项,但没有选择。下面是我当前正在使用的代码片段: //Definition of Enum: public enum eNumericAndDat

我遇到了一个问题,模型中绑定到枚举的几个组合无法正常工作。首先,让我告诉你,我的应用程序有两个区域可以操作枚举。为清晰/简单起见,我们只需说,左侧和右侧有相同的视图,中间有结果/摘要。当我在其中一个视图中设置值时,它通过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
   }
//枚举的定义:
公共枚举EnumericandDate运算符
{GreaterThan,GreaterThanOrEqualTo,Equals,LessThanOrEqualTo,LessThan,LessThan,Between,Ignore}
//视图中组合的XAML:
//枚举转换器代码
公共类EnumToBoolConverter:IValueConverter
{
#区域方法
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
if(值==null | |参数==null)
返回值;
返回值.ToString()==参数.ToString();
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
if(值==null | |参数==null)
返回值;
返回Enum.Parse(targetType,parameter.ToString(),true);
}
#端域法
}
EnumConverter是基本的Enum-to-Bool转换器类(是,定义了ConvertBack)


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

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

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 { ... } }
}
公共类运算符
{
公共字符串密钥{get;set;}
公共字符串标题{get;set;}
...
大于{get{…}的公共静态运算符
公共静态运算符LessThan{get{…}
公共静态IList运算符{get{…}
}
在ViewModel中,您添加了一个引用static Operator.Operators属性的Operators属性

在你看来:

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


然后为显示标题的Operator类创建一个DataTemplate。这种方法的优点是,您可以轻松地将新函数添加到运算符类中-使用枚举,您将始终受到限制

,尽管我仍然有一个问题,但这或多或少是我提出的解决方案,这不可能不起作用,但不知怎的,确实如此。但是我正在使用SelectedValue属性。。。所以我会给你一个公认的答案。