title: Creating a checkable context menu from a list of enum values
body: <p>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.</p>
  
  <p>I have a certain enum type (let's call it <code>MyCustomEnum</code>) which I'd like to use to create the list of choices in a <code>ContextMenu</code>.  I'd like the menu items to be checkable, and have the checked <code>MenuItem</code> bound to a static setting the application uses (a property we'll call <code>MyCustomEnumSetting</code> on the settings class <code>MyCustomSettingsClass</code>).</p>
  
  <p>So far, I'm able to generate the <code>ContextMenu</code> and check the correct <code>MenuItem</code>, based on the setting value.  I did this using a <code>MultiBinding</code> to compare the enum value in the <code>DataContext</code> of the <code>MenuItem</code> 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 <code>MenuItem</code> 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.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>&lt;UserControl&gt;
      &lt;UserControl.Resources&gt;
          &lt;ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type system:Enum}" x:Key="MyCustomEnumProvider"&gt;
              &lt;ObjectDataProvider.MethodParameters&gt;
                  &lt;x:Type Type="local:MyCustomEnum" /&gt;
              &lt;/ObjectDataProvider.MethodParameters&gt;
          &lt;/ObjectDataProvider&gt;
          &lt;local:EnumEqualsConverter x:Key="EnumEqualsConverter" /&gt;
      &lt;/FrameworkElement.Resources&gt;
     &lt;FrameworkElement.ContextMenu&gt;
          &lt;ContextMenu ItemsSource="{Binding Source={StaticResource MyCustomEnumProvider}}"&gt;
              &lt;ContextMenu.ItemContainerStyle&gt;
                  &lt;Style TargetType="MenuItem"&gt;
                      &lt;Setter
                          Property="IsChecked"&gt;
                          &lt;Setter.Value&gt;
                              &lt;MultiBinding Converter="{StaticResource EnumEqualsConverter}"&gt;
                                  &lt;MultiBinding.Bindings&gt;
                                      &lt;!--First binding source is the application setting value--&gt;
                                      &lt;Binding Source="{x:Static local:MyCustomSettingsClass.Default}" Path="MyCustomEnumSetting" /&gt;
                                      &lt;!--Second binding source is the enum value in the data context of the MenuItem--&gt;
                                      &lt;Binding RelativeSource="{RelativeSource Self}" Path="DataContext" Mode="OneWay" /&gt;
                                  &lt;/MultiBinding.Bindings&gt;
                              &lt;/MultiBinding&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/ContextMenu.ItemContainerStyle&gt;
          &lt;/ContextMenu&gt;
      &lt;/FrameworkElement.ContextMenu&gt;
  &lt;/FrameworkElement&gt;
  </code></pre>
  
  <p>And the code for my <code>IMultiValueConverter</code>:</p>
  
  <pre><code>public sealed class EnumEqualsConverter : IMultiValueConverter
  {
      object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
          IEnumerable&lt;Enum&gt; enums = values.Cast&lt;Enum&gt;();
          var value1 = enums.ElementAt(0);
          var value2 = enums.ElementAt(1);
          return value1.Equals(value2);
      }
  
      object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>Any suggestions for a different direction I could go to get the results I'm looking for?</p>