1

I have an enum with various values:

public enum UserStatus
{
    Active = 1,
    Inactive = 2,
    Invalid = 3,
    Blocked = 4,
    Pending = 5
}

And on my UI I'm assigning a color for each value of the enum, and since it's used in various windows, I've created a Converter for it.

Now I want to display a legend of some of the enum values, is there anyway I can bind a static enum value to a property in a WPF control?

<!-- I want ? to be a fixed enum value -->
<TextBlock Text="{Binding ?, Converter={StaticResource=UserStatusToString}}" Foreground={Binding ?, Converter={StaticResource=UserStatusToBrush}} />

I don't have a data object at this point and I only want to somehow pick the color value from the converter instead of hard-typing it in the legend. Is there anyway I can do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Danicco
  • 1,573
  • 2
  • 23
  • 49

2 Answers2

1

I think this Stack Overflow post might be your answer. You'll have to have your enum value listed in your xaml's resources section and use its key as StaticResource key.

Community
  • 1
  • 1
Andrej Mohar
  • 966
  • 8
  • 20
0

Use ObjectDataProvider to get all enum values and then show them in ListBox with ItemTemplate

<Window xmlns:local="clr-namespace:FooApp" ... >
    <Window.Resources>
        <ObjectDataProvider x:Key="FooEnumValues" 
            MethodName="GetValues"
            ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:Foo" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource FooEnumValues}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock 
    Text="{Binding Converter={StaticResource=UserStatusToString}}" 
    Foreground="{Binding Converter={StaticResource=UserStatusToBrush}}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
Posix
  • 300
  • 3
  • 14