2014-10-01 48 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

而不是使用转换器绑定项目源,然后选择项目, 您可以尝试只设置组合框项目的项目模板以显示友好名称。