簡體   English   中英

使用IValueConverter綁定到Enum的ComboBox丟失SelectedItem

[英]ComboBox bound to Enum using IValueConverter loses SelectedItem

我有一個綁定到枚舉的組合框,效果很好,但是當我使用IValueConverter使枚舉值更友好時,組合框中不再有默認的選定項。 有任何想法嗎?

[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>

                            ...

也將Converter應用於您的SelectedItem綁定。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM