2014-10-01 47 views
1

我有一個組合框綁定到枚舉,它工作正常,但是當我使用IValueConverter使枚舉值更友好,組合框中不再默認選定的項目。有任何想法嗎?組合框綁定到Enum使用IValueConverter失去SelectedItem

[ValueConversion(typeof(InstallerAction), typeof(string))] 
    public class InstallerActionConverter : IValueConverter 
    { 
     /// <summary> 
     /// Converts a value. 
     /// </summary> 
     /// <param name="value">The value produced by the binding source.</param> 
     /// <param name="targetType">The type of the binding target property.</param> 
     /// <param name="parameter">The converter parameter to use.</param> 
     /// <param name="culture">The culture to use in the converter.</param> 
     /// <returns> 
     /// A converted value. If the method returns null, the valid null value is used. 
     /// </returns> 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var installerActions = (InstallerAction[])value; 
      var result = new List<string>(); 

      foreach(var item in installerActions) 
      { 
       switch (item) 
       { 
        case InstallerAction.Install: 
         result.Add("Install"); 
         break; 
        case InstallerAction.None: 
         result.Add("None"); 
         break; 
        case InstallerAction.UninstallAll: 
         result.Add("Uninstall All"); 
         break; 
        case InstallerAction.UninstallOne: 
         result.Add("Uninstall One"); 
         break; 
       } 
      } 
      return result; 
     } 

     /// <summary> 
     /// Converts a value. 
     /// </summary> 
     /// <param name="value">The value that is produced by the binding target.</param> 
     /// <param name="targetType">The type to convert to.</param> 
     /// <param name="parameter">The converter parameter to use.</param> 
     /// <param name="culture">The culture to use in the converter.</param> 
     /// <returns> 
     /// A converted value. If the method returns null, the valid null value is used. 
     /// </returns> 
     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var installerActions = (string)value; 

      switch (installerActions) 
      { 
       case "None": 
        return InstallerAction.None; 
       case "Install": 
        return InstallerAction.Install; 
       case "Uninstall One": 
        return InstallerAction.UninstallOne; 
       case "Uninstall All": 
        return InstallerAction.UninstallAll; 
       default: 
        return InstallerAction.None; 
      } 
     } 
    } 

而XAML:

<ListView Name="ListView" ItemsSource="{Binding DatabaseInfos}" VerticalAlignment="Stretch" Margin="10"> 
        <ListView.Resources> 
         <Style TargetType="ListViewItem"> 
          <Style.Triggers> 
           <Trigger Property="Validation.HasError" Value="True"> 
            <Setter Property="Background" Value="Red" /> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </ListView.Resources> 
        <ListView.View> 
         <GridView> 
          <GridViewColumn Header="Action"> 
           <GridViewColumn.CellTemplate> 
            <DataTemplate> 
             <ComboBox Width="100" ItemsSource="{Binding Source={StaticResource ActionFromEnum}, Converter={StaticResource InstallerActionConverter}}" SelectedItem="{Binding Action}" /> 
            </DataTemplate> 
           </GridViewColumn.CellTemplate> 
          </GridViewColumn> 

          ... 
+0

ItemsSource是字符串類型,但SelectedItem仍然綁定到枚舉。兩者應該是一致的,即如果ItemsSource是「IEnumerable 」類型,則所選項目應該是「T」類型。 – 2014-10-01 18:19:08

回答

1

應用轉換到您的SelectedItem結合爲好。

+0

我最終使用了兩個轉換器:一個用於'ItemsSource'('MyEnum []'),另一個用於'SelectedItem'('MyEnum') – 2014-10-02 13:24:10

0

而不是使用轉換器綁定項目源,然後選擇項目, 您可以嘗試只設置組合框項目的項目模板以顯示友好名稱。