<p>I'd like to make some custom MenuHeaders in WPF so I can have (for example), an icon and text in a menu item.</p>

<p>Normally using MenuItems, if you populate the Header field with straight text, you can add an accelerator by using an underscore. eg, _File</p>

<p>However, if I wanted to put in a UserControl, I believe this function would break, how would I do something similar to the following?</p>

<pre><code>&lt;Menu&gt;
&lt;MenuItem&gt;
    &lt;MenuItem.Header&gt;
    &lt;UserControl&gt;
        &lt;Image Source="..." /&gt;
        &lt;Label Text="_Open" /&gt;
    &lt;/UserControl&gt;
    &lt;/MenuItem.Header&gt;
&lt;/MenuItem&gt;
...
</code></pre>

[{'answer_id': 59706, 'author': 'Alan Le', 'author_id': 1133, 'author_profile': 'https://Stackoverflow.com/users/1133', 'pm_score': 2, 'selected': False, 'text': '<p>The problem is you placed the image inside of the content of the MenuHeader which means that you\'ll lose the accelerator key. If you\'re just trying to have an image in the menu header, do the following.</p>\n\n<pre><code>&lt;MenuItem Header="_Open"&gt;\n  &lt;MenuItem.Icon&gt;\n    &lt;Image Source="images/Open.png"/&gt;\n  &lt;/MenuItem.Icon&gt;\n&lt;/MenuItem&gt;\n</code></pre>\n\n<p>If you want to customize the look and feel even further, modify the <a href="http://msdn.microsoft.com/en-us/library/ms747082(VS.85).aspx" rel="nofollow noreferrer">controltemplate</a> and style for the menu. From experience, styling the menus and menuitems are much more difficult then styling the other WPF controls.</p>\n'}, {'answer_id': 60361, 'author': 'Nidonocu', 'author_id': 483, 'author_profile': 'https://Stackoverflow.com/users/483', 'pm_score': 0, 'selected': False, 'text': "<p>@a7an: Ah, I didn't notice the Icon property before. That's a good start.</p>\n\n<p>However, specifically I wanted to add an extra 'button' to some MenuItems so I could have a 'Pin' feature (see the recently loaded Documents list in Office 2007 for the feature idea).</p>\n\n<p>Since there needs to be code as well, will I probably need to subclass the control and add the code for the button? (Not affraid of messing with the MenuItem template, have already had to do it once and I'd do it again if I had to! ;) )</p>\n"}, {'answer_id': 60365, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 4, 'selected': True, 'text': '<p>I think the Icon property fits your needs.<br>\nHowever to answer the original question, it is possible to retain the Accelerator functionality when you compose the content of your menuitem. <strong>If you have nested content in a MenuItem you need to define the AccessText property explicitly</strong> like in the first one below. When you use the inline form, this is automagically taken care of.</p>\n\n<pre><code> &lt;Menu&gt; \n   &lt;MenuItem&gt;\n      &lt;MenuItem.Header&gt;\n        &lt;StackPanel Orientation="Horizontal"&gt;\n          &lt;Image Source="Images/Open.ico" /&gt;      \n          &lt;AccessText&gt;_Open..&lt;/AccessText&gt;\n        &lt;/StackPanel&gt;\n      &lt;/MenuItem.Header&gt;\n    &lt;/MenuItem&gt;\n    &lt;MenuItem Header="_Close" /&gt;\n   &lt;/Menu&gt;\n</code></pre>\n'}, {'answer_id': 1108605, 'author': 'awe', 'author_id': 109392, 'author_profile': 'https://Stackoverflow.com/users/109392', 'pm_score': 2, 'selected': False, 'text': '<p>First thought, you would think that the Icon property can only contain an image. But it can actually contain anything! I discovered this by accident when I programmatically tried to set the Image property directly to a string with the path to an image. The result was that it did not show the image, but the actual text of the path! Then I discovered that I had to create an Image element first and set that to the Icon property. This lead me to think that the Image property was just any content container that is located in the icon area at the left in the menu, and I was right. I tried to put a button there, and it worked!</p>\n\n<p>This is showing a button with the text "i" in the Icon area of the menu item. When you click on the button, the Button_Click event is triggered (the LanguageMenu_Click is NOT triggered when you click the button).</p>\n\n<pre><code>&lt;MenuItem Name="LanguageMenu" Header="_Language" Click="LanguageMenu_Click"&gt;\n  &lt;MenuItem.Icon&gt;\n    &lt;Button Click="Button_Click"&gt;i&lt;/Button&gt;\n  &lt;/MenuItem.Icon&gt;\n&lt;/MenuItem&gt;\n</code></pre>\n\n<p>This leads to an alternative to not have to make an image for the icon, but use text with a symbol font instead to display a simple "icon". The following example uses the Wingdings font which contains a floppydisk symbol. This symbol in the font is mapped to the charachter <code>&lt;</code>, which has special meaning in XAML, so we have to use the encoded version <code>&amp;lt;</code> instead. This works like a dream! The following shows a floppydisk symbol as an icon on the menu item: </p>\n\n<pre><code>&lt;MenuItem Name="mnuFileSave" Header="Save" Command="ApplicationCommands.Save"&gt;\n  &lt;MenuItem.Icon&gt;\n    &lt;Label VerticalAlignment="Center" HorizontalAlignment="Center" \n           FontFamily="Wingdings"&gt;&amp;lt;&lt;/Label&gt;\n  &lt;/MenuItem.Icon&gt;                \n&lt;/MenuItem&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59456', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/483/']