开发者

MouseOver Trigger firing on ContextMenu with overridden ControlTemplate. Where is it coming from?

开发者 https://www.devze.com 2022-12-22 17:12 出处:网络
I have this very simple ControlTemplate: <ControlTemplate TargetType=\"{x:Type ContextMenu}\"> <Border

I have this very simple ControlTemplate:

<ControlTemplate TargetType="{x:Type ContextMenu}">
                    <Border 
                           Name="Border"
                           Background="{StaticResource BlueBackground}"
                           BorderBrush="LightBlue"
                           CornerRadius="10"
                           BorderThickness="1" >
                        <StackPanel IsItemsHost="True"/>
                    </Border>
                </ControlTemplate>

I made it to create a开发者_开发百科 nifty jawdroppingly beautiful rounded corner! However, when I point the mouse over a contextmenu a MouseOver Trigger fires from somewhere that draws a terribly ugly nearly square border on top of my nifty rounded border!

Where is it coming from??

EDIT: The most likely cause is that the ContextMenu is an ItemsControl that holds MenuItems, even when my ContextMenu holds a single UserControl. So the UserControl is seen as a MenuItem and highlighted when the IsMouseOver==true! What is the easiest way to disable this behaviour?


You can set the ItemContainerStyle property of your ContextMenu to a custom style for MenuItems.

<ContextMenu.ItemContainerStyle>
    <Style
        TargetType="MenuItem">
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="MenuItem">
                    <TextBlock
                        Text="{TemplateBinding Header}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ContextMenu.ItemContainerStyle>

With this strategy you have to make your own triggers for any mouse over effects you want, but Click and Checked events will still fire normally.

0

精彩评论

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