开发者

Silverlight Databinding a Combobox to an Enum

I have run into a problem where a couple of combos that are 开发者_如何学运维bound to enums in my model are not working properly. To start off let me tell you that I have 2 areas of my app where the enum can be manipulated. For clarity/simplicity, lets just say there are identicle views on the left and right and the results/summary in the center. When I set the value in one of the views it sets the value through the ViewModel to the Model, as designed, but the combo in the view on the opposite side (again just for clarity/simplicity's sake) doesn't seem to have updated. It should have selected the appropriate row/item, but doesn't. Below are snippets of code that I'm currently using:

//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 is your basic Enum to Bool converter class (and yes with ConvertBack defined).

Should I be using the Selected Index on the combo instead? I can't really load the enum as the combo's ItemSource through an EnumToIEnumerableConverter or something because I want to customize the visable text for each item...


Why don't you turn your enum into a simple class that has static properties

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 { ... } }
}

In your ViewModel you add an Operators property that references the static Operator.Operators property

In your view:

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

Then create a DataTemplate for the Operator class that displays the Caption. The advantage of this approach is that you can easily add new funcationality to your operator class - with an enum you will always be limited

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜