2

I have normal menu in XAML. And I would like to completely change looks of the popup that gets displayed when you click on menu item.

I'm looking for something like this:

<Menu>
    <MenuItem Header="MyCustomMenu">
        <MenuItem.PopupTemplate>
            <ControlTemplate>
                <Image Source="SexyImage"/>
            </ControlTemplate>
        </MenuItem.PopupTemplate>
    </MenuItem>
</Menu>
Kugel
  • 19,354
  • 16
  • 71
  • 103
  • Popup is your custom control? I can't get the question. Or you mean ContextMenu control as popup? – sll Sep 09 '11 at 12:48
  • It's an example. By popup i mean the context menu that literaly pops up when you click on menu. However I don't want it to be ContextMenu I want to be able to put anything on there. Let's say an image. – Kugel Sep 09 '11 at 12:56

2 Answers2

2

You need to override the template of the MenuItem since the Popup is a part of it, have a look at the default templates to get an idea of what a template should look like. MenuItem has a TemplatePartAttribute for said popup.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
1

Define following styles in resources of your application:

An example of style overriding for MenuItem:

<Style TargetType="MenuItem">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="MenuItem">
                       .... here is your custom template

ContextMenu styles overriding:

<Style TargetType="ContextMenu">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ContextMenu">
sll
  • 61,540
  • 22
  • 104
  • 156
  • Ok, this is a good way to customize ContextMenu and items within it. Thanks for code. It is helpful. But this is not exactly what I'm after. – Kugel Sep 12 '11 at 11:04
  • @Kugel : have you found a solution for your task? – sll Sep 12 '11 at 11:07
  • I think H.B. suggenstion will work. I'm giving it a try. I'll post results later. – Kugel Sep 12 '11 at 11:08
  • @Kugel : I believe my example also overriding MenuItem template by specifying setter for property Template and providing ControlTemplate for menuItem type, am I right? – sll Sep 12 '11 at 11:23