开发者

How can I completely customize popup from MenuItem?

开发者 https://www.devze.com 2023-04-04 06:19 出处:网络
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 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>


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.


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">
0

精彩评论

暂无评论...
验证码 换一张
取 消