2010-11-22 13 views
1

単純な値を持つ単純なComboBoxがあります。私は2ウェイバインディングを私のモデル上のenumプロパティで行うことを試みています。ここで WPF ComboBox 2-Wayバインディング

<ComboBox d:LayoutOverrides="Height" Grid.Column="1" SelectedItem="{Binding SortType, Converter={StaticResource sortSelect}, Mode=TwoWay}"> 
     <ListBoxItem Content="Ascending" Tag="Ascending"/> 
     <ListBoxItem Content="Descending" Tag="Descending"/> 
     <ListBoxItem Content="Absolute Ascending" Tag="AbsoluteAscending"/> 
     <ListBoxItem Content="Absolute Descending" Tag="AbsoluteDescending" /> 
    </ComboBox> 

は私 ValueConverter

public class RdiSortMatchConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var val = (RdiSort) value; 
     switch (val) 
     { 
      case RdiSort.Ascending: 
       return "Ascending"; 
      case RdiSort.Descending: 
       return "Descending"; 
      case RdiSort.AbsoluteAscending: 
       return "Absolute Ascending"; 
      case RdiSort.AbsoluteDescending: 
       return "Absolute Descending"; 
      default: 
       throw new ArgumentOutOfRangeException(); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return (RdiSort) Enum.Parse(typeof (RdiSort), (string) ((ListBoxItem) value).Tag); 
    } 
} 

ConvertBackメソッドが正常に動作している、と私のモデルはListBoxItemTag値に基づいて更新されますが、私は正しいListBoxItemを選択するために、初期Enum値を取得カント

これを達成するための最良の方法は何ですか、またはより良いバインディング方法がありますか?EnumsEnum値ごとにエスケープします。

+0

は、この質問を参照してください:[WPFのコンボボックスに列挙型プロパティをデータバインディング] (http://stackoverflow.com/questions/58743/databinding-an-enum-property-to-a-combobox-in-wpf) –

答えて

2

このようにすることができます。まず、あなたの列挙型のそれぞれが

public enum RdiSort 
{ 
    [Description("Ascending Description")] 
    Ascending, 
    [Description("Descending Description")] 
    Descending, 
    [Description("AbsoluteAscending Description")] 
    AbsoluteAscending, 
    [Description("AbsoluteDescending Description")] 
    AbsoluteDescending 
} 

値次に、あなたのコンボボックスでRdiSortValuesプロバイダを使用して見ることのTextBlockとコンバータとのDataTemplateを作成

xmlns:sys="clr-namespace:System;assembly=mscorlib" 
xmlns:MyEnumerations="clr-namespace:MyEnumerations" 

<ObjectDataProvider MethodName="GetValues" 
        ObjectType="{x:Type sys:Enum}" 
        x:Key="RdiSortValues"> 
    <ObjectDataProvider.MethodParameters> 
     <x:Type TypeName="MyEnumerations:RdiSort" /> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 

あなたのコンボボックスのためたObjectDataProviderを使用するための説明を追加Enum値の代わりに説明。

<local:EnumDescriptionConverter x:Key="EnumDescriptionConverter"/> 

<ComboBox SelectedItem="{Binding SortType}" 
      ItemsSource="{Binding Source={StaticResource RdiSortValues}}"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

最後にコンバータです。コンバータはTextBlockでのみ表示されるため、ConvertBackは必要ありません。

public class EnumDescriptionConverter : IValueConverter 
{ 
    private string GetEnumDescription(Enum enumObj) 
    { 
     FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString()); 
     object[] attribArray = fieldInfo.GetCustomAttributes(false); 
     if (attribArray.Length == 0) 
     { 
      return enumObj.ToString(); 
     } 
     else 
     { 
      DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute; 
      return attrib.Description; 
     } 
    } 

    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value != null) 
     { 
      RdiSort myEnum = (RdiSort)value; 
      string description = GetEnumDescription(myEnum); 
      return description; 
     } 
     return null; 
    } 
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return string.Empty; 
    } 
} 
0

ここに私が行った解決策があります。

の.xaml =>

<UserControl.Resources> 
    <c:PriorityConvertor x:Key="priorityConvertor"></c:PriorityConvertor> 
</UserControl.Resources> 

の.csファイル=>

public class PriorityConvertor : IValueConverter 
{ 

    #region IValueConverter Members 

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 
      return string.Empty; 
     } 
     if (value.ToString() == "1") 
     { 
      return "High"; 
     } 
     else if (value.ToString() == "2") 
     { 
      return "Medium"; 
     } 
     else if (value.ToString() == "3") 
     { 
      return "Low"; 
     } 
     return "Low"; 
    } 

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

}

関連する問題