Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox Converter for Enumerations

I have an enumeration that has values like

HomeRun, StolenBase, FirstBase, etc.

I want to display these values in a combobox, with a space inserted before the capital letters, so it will display as 'Home Run', 'Stolen Base', etc.

I already have code that can do the formatting for me, and I have added that code to the 'Convert' method of an IValueConverter implementation.

My question is, where do I need to use this converter (in the xaml) such that not only the dropdown, but also the displayed value, will have this formatting? Do I need to implement ConvertBack as well?

I am well aware of setting 'descriptions' for the enumeration and using the popular EnumToDescriptionConverter, but I'd rather stay away from that.

like image 749
user981225 Avatar asked Jan 19 '23 10:01

user981225


2 Answers

I'm not sure if there is a better way, but you can achieve what you want using an ItemTemplate

<ComboBox.ItemTemplate>
    <DataTemplate>
        <ContentPresenter
            Content="{Binding Converter={StaticResource baseballEnumConverter}}"/>
     </DataTemplate>
</ComboBox.ItemTemplate>

This will display the converted value in your ComboBox.

The SelectedValue of the ComboBox will still be the Enum value. You won't need to implement ConvertBack.

like image 96
wdavo Avatar answered Jan 20 '23 23:01

wdavo


[updated] The key point of my answer is that the enum values are converted totally. I think this way is eaier than the coverting each enum value.[/updated]

Where do I need to use this converter (in the xaml) such that not only the dropdown, but also the displayed value, will have this formatting?

At Binding ItemsSource of ComboBox(ItemsSource="{Binding Source={x:Null}, Converter={StaticResource converter}}"), Please check the following code.

Do I need to implement ConvertBack as well?

No, you don't., because at runtime you cannot modify the enumeration, and even though it can do, you cannot change ItemsSource of ComboBox in VIEW, which means Binding Mode is OneWay.

XAML

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MyEnumConverter x:Key="converter"/>
    </Window.Resources>
    <StackPanel>
        <ComboBox ItemsSource="{Binding Source={x:Null}, Converter={StaticResource converter}, Mode=OneWay}"></ComboBox>
    </StackPanel>
</Window>

Code

public enum MyEnum
{
    HomeRun, StolenBase, FirstBase
}

[ValueConversion(typeof(object), typeof(List<string>))]
public class MyEnumConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var names = Enum.GetNames(typeof (MyEnum)).ToArray();
        //Add some code to support the thing you want to do(add blank in front of Capital...)
        return names;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
like image 30
Jin-Wook Chung Avatar answered Jan 20 '23 22:01

Jin-Wook Chung