WPF menu item with image

前端 未结 3 678
死守一世寂寞
死守一世寂寞 2021-01-01 08:23

How to define MenuItem.Icon so that the MenuItem

相关标签:
3条回答
  • 2021-01-01 08:54

    The easy way way is to not use the Icon property but to instead put the icon in the Header:

    <Menu>
      <MenuItem>
        <MenuItem.Header>
          <StackPanel>
            <Image Width="20" Height="20" Source="/XSoftArt.WPFengine;component/Images/export32x32xp.png" />
            <ContentPresenter Content="Reports" />
          </StackPanel>
        </MenuItem.Header>
      </MenuItem>
      <MenuItem Header="Export" />
      <MenuItem Header="New record" />
    </Menu>
    

    For this simple case the <ContentPresenter Content="Reports" /> can be replaced with a <TextBlock Text="Reports" /> because that's what ContentPresenter would use to present the string anyway. For more complex Header=, you could use the ContentPresenter as shown.

    0 讨论(0)
  • 2021-01-01 08:57

    How something along the lines of:

    <ContextMenu>
        <MenuItem Header="Reports">
            <MenuItem.Icon>
                <Image Source="/XSoftArt.WPFengine;component/Images/export32x32xp.png"/>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
    
    0 讨论(0)
  • 2021-01-01 09:13

    In the case of StackPanel use Label and not the TextBlock since only Label will allow you to have the mnemonics on the menu, like _Reports.

    0 讨论(0)
提交回复
热议问题