2

I need to display all enum values as tab items on TabControl, except one enum member (None)

enum MyEnum { Value1, Value2, Value3, None }

TabControl should display three tabs (Value1, Value2 and Value3). I need to be able to get/set current tab in the ViewModel by binding to SelectedItem property. Header for each tab uses localized enum value, which currently I do using converter.

Is this possible? I have tried many things, but could not make it work. I have no trouble with manually adding each TabItem, but I am unsure how to make SelectedItem (tab) to be of enum type.

Goran
  • 6,328
  • 6
  • 41
  • 86
  • This can be done in case of a ComboBox: http://stackoverflow.com/questions/58743/databinding-an-enum-property-to-a-combobox-in-wpf, http://www.codeproject.com/Articles/29495/Binding-and-Using-Friendly-Enums-in-WPF – kol Jun 20 '12 at 23:08
  • I am looking at the post you have given, and I don't see how it is related to my question. It only gives information how to bind to enum to display ALL members, what I need to do is exclude one member from it. I have no problem in binding a list control to enum and displaying localized values, that is easy. – Goran Jun 20 '12 at 23:12

2 Answers2

1

I found the solution. Normally you would do this in order to get a list of enum members:

<ObjectDataProvider x:Key="SomeEnumValues"
                    MethodName="GetValues" 
                    ObjectType="{x:Type sys:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="vm:SomeEnum" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

This would get all the members, so its not a way to go. I have created a custom converter that will accept enum value, and extract all members from type

// in the convert method
Type type = value.GetType();

List<object> enumValues = new List<object>();

// add each member, except None (has value of 0)
foreach (var field in type.GetFields())
{
    if (field.IsLiteral)
    {
        var x = field.GetValue(null);

        // add new value
        if ((int)x != 0)
            enumValues.Add(field.GetValue(null));
    }
}

Localization is done in the ItemTemplate. In there I also use converter (different one), where I get the localized value.

<DataTemplate x:Key="EnumItemTemplate">
    <TextBlock Text="{Binding Mode=OneWay, Converter={StaticResource enumConverter}}"/>
</DataTemplate>
Goran
  • 6,328
  • 6
  • 41
  • 86
0

Offhand, I do not think there is a way to do what you are trying to do. SelectedItem is going to be a TabItem object, and I don't think there's a way to use a binding converter here to trick it into using an enum. Even if you could do it, there's no simple way to have it magically recognize that one of the enum values is one you don't want to bind to (i.e. "None").

Your best bet is to bind the Tag property of each TabItem to the enum you want, and use code to achieve the selection effect you want.

Alan McBee
  • 4,202
  • 3
  • 33
  • 38
  • Actually, SelectedItem is going to be a TabItem object, and if we bind to Enum, TabItem will be an enum value. I think I am close to a solution. – Goran Jun 20 '12 at 23:55