Thursday, 12 September 2013

Creating a checkable context menu from a list of enum values

Creating a checkable context menu from a list of enum values

I see lots of people asking questions similar to mine, but so far I
haven't been able to put the pieces together to solve my problem.
I have a certain enum type (let's call it MyCustomEnum) which I'd like to
use to create the list of choices in a ContextMenu. I'd like the menu
items to be checkable, and have the checked MenuItem bound to a static
setting the application uses (a property we'll call MyCustomEnumSetting on
the settings class MyCustomSettingsClass).
So far, I'm able to generate the ContextMenu and check the correct
MenuItem, based on the setting value. I did this using a MultiBinding to
compare the enum value in the DataContext of the MenuItem to the value of
the settings class enum and see if their values equal. However, the
binding is only one way: clicking on an unchecked MenuItem doesn't update
the binding. I have a feeling I'm missing something, but this part of WPF
is a bit more fuzzy to me.
Here's what I have so far:
<UserControl>
<UserControl.Resources>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type
system:Enum}" x:Key="MyCustomEnumProvider">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:MyCustomEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:EnumEqualsConverter x:Key="EnumEqualsConverter" />
</FrameworkElement.Resources>
<FrameworkElement.ContextMenu>
<ContextMenu ItemsSource="{Binding Source={StaticResource
MyCustomEnumProvider}}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter
Property="IsChecked">
<Setter.Value>
<MultiBinding Converter="{StaticResource
EnumEqualsConverter}">
<MultiBinding.Bindings>
<Binding Source="{x:Static
local:MyCustomSettingsClass.Default}"
Path="MyCustomEnumSetting" />
<Binding
RelativeSource="{RelativeSource Self}"
Path="DataContext" Mode="OneWay" />
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</FrameworkElement.ContextMenu>
</FrameworkElement>
In this example, EnumEqualsConverter simply checks whether the input
values are equal. Any suggestions for a different direction I could go to
get the results I'm looking for?

No comments:

Post a Comment