Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set a MenuItem's IsChecked property for a child of type Enum via XAML?
    primarykey
    data
    text
    <p>I've bound a MenuItem to a Enum using <a href="https://stackoverflow.com/questions/3807724">this solution</a>. </p> <p>The Enum Values are displayed correctly, yet I cannot seem to set a default checked value for the MenuItem's children items. </p> <p>Alternatively put, I would like the MenuItem to have one of its children items (the values of the enum I'm using) checked per default.</p> <p>I've tried the following code, using a Style and a triggered Setter :</p> <pre><code>&lt;ContextMenu&gt; &lt;MenuItem Header="Some Setting" Name="SomeSettingMenu" DataContext="{Binding}" ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}" Click="SomeSettingClicked"&gt; &lt;MenuItem.ItemContainerStyle&gt; &lt;Style TargetType="MenuItem"&gt; &lt;Setter Property="MenuItem.IsCheckable" Value="True"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="MenuItem.Header" Value="enums:AnEnum.ItemA" &gt; &lt;Setter Property="MenuItem.IsChecked" Value="True"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/MenuItem.ItemContainerStyle&gt; &lt;/MenuItem&gt; &lt;/ContextMenu&gt; </code></pre> <p>The enum contains values such as ItemA, I've also tried <code>AnEnum.First</code> or <code>0</code> in the Trigger Value attribute (as <a href="https://stackoverflow.com/questions/1165402/question-on-initial-value-of-an-enum-c">answered here</a>), to no avail.</p> <p>Would a <code>DataTrigger</code> be advisable ? If so, how can I write that in XAML ? Or should I use an <code>DataTemplate</code> within an <code>ItemTemplate</code> for the <code>MenuItem</code> ?</p> <p>I've also tried fiddling around in code-behind with <code>SomeSetting.Items</code> -related methods, yet most of the properties (such as Current) are read-only. </p> <p>I am aware that you can write <code>SomeSettingMenu.ItemsSource = Enum.GetValues(typeof(....))</code> in code-behind, but yet again I don't know how to select an Item within the MenuItem programmatically.</p> <p>I've also tried this code, doesn't work as well :</p> <pre><code>&lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=Header}" Value="enums:DisplayType.ItemA"&gt; &lt;Setter Property="IsChecked" Value="True" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; </code></pre> <p><code>enums</code> is a namespace from a different assembly I'm using.</p> <p>Any thoughts would be appreciated, thank you in advance !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload