How to create custom MenuHeaders in WPF using accelerators?

I would like to make some custom MenuHeaders in WPF so that I can have (for example) an icon and text in a menu item.

Usually with MenuItems, if you fill out the title bar with plain text, you can add an accelerator using the underscore. e.g. _File

However, if I wanted to introduce UserControl, I think this function will break, how would I do something like the following?

<Menu>
<MenuItem>
    <MenuItem.Header>
    <UserControl>
        <Image Source="..." />
        <Label Text="_Open" />
    </UserControl>
    </MenuItem.Header>
</MenuItem>
...
+3
source share
4 answers

, Icon .
, , , . MenuItem, AccessText, . , .

 <Menu> 
   <MenuItem>
      <MenuItem.Header>
        <StackPanel Orientation="Horizontal">
          <Image Source="Images/Open.ico" />      
          <AccessText>_Open..</AccessText>
        </StackPanel>
      </MenuItem.Header>
    </MenuItem>
    <MenuItem Header="_Close" />
   </Menu>
+7

, MenuHeader, , . , .

<MenuItem Header="_Open">
  <MenuItem.Icon>
    <Image Source="images/Open.png"/>
  </MenuItem.Icon>
</MenuItem>

, controltemplate . , , WPF.

+4

, Icon . ! , Image . , ! , Image Icon. , Image , , . , !

"i" . , Button_Click ( LanguageMenu_Click .)

<MenuItem Name="LanguageMenu" Header="_Language" Click="LanguageMenu_Click">
  <MenuItem.Icon>
    <Button Click="Button_Click">i</Button>
  </MenuItem.Icon>
</MenuItem>

, , , . Wingdings, floppydisk. charachter <, XAML, &lt;. ! floppydisk :

<MenuItem Name="mnuFileSave" Header="Save" Command="ApplicationCommands.Save">
  <MenuItem.Icon>
    <Label VerticalAlignment="Center" HorizontalAlignment="Center" 
           FontFamily="Wingdings">&lt;</Label>
  </MenuItem.Icon>                
</MenuItem>
+2

@a7an: Icon. .

However, I wanted to add an additional 'button' to some MenuItems so that I had a "Pin" function (see the recently uploaded document list in Office 2007 for an idea of ​​the function).

Since you need code, maybe I will probably need to subclass the control and add code for the button? (Don’t be afraid to mess with the MenuItem template, he already had to do it once, and I would do it again if I had to!;))

0
source

Source: https://habr.com/ru/post/1696834/


All Articles