0
votes

I have styled my Menu/ContextMenu/MenuItem controls in App.xaml so that those styles apply to all my app.

defined like this (e.g. with the MenuItem):

<Style TargetType="{x:Type MenuItem}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <!-- more setters -->
</Style>

this is working great.

now, at some point I have a RichTextBox, and I would like its ContextMenu and MenuItems to have a different style.

So I wrote:

<RichTextBox>

    <RichTextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Command="Undo" Style="{StaticResource menuItem}">Toto</MenuItem>
            <MenuItem Command="Redo"/>
            <Separator/>
            <MenuItem Command="Cut"/>
            <MenuItem Command="Copy"/>
            <MenuItem Command="Paste"/>
            <MenuItem Command="SelectAll"/>
        </ContextMenu>
    </RichTextBox.ContextMenu>

    <!-- and here the RichTextBox's conent -->

</RichTextBox>

and now I'm trying to figure out where I can put my new style... Problem is: I can't figure it out: no matter where and How I add it (static ressource on the RichTextBox, or on The ContextMenu, with an explicit Key or just a targetType), I cannot get rid of my "Global" style. And the local one is just ignored.

how can I proceed?

2

2 Answers

1
votes

Try:

        <RichTextBox.Resources>
            <Style x:Key="menuItem"  TargetType="{x:Type MenuItem}">
                <Setter Property="Foreground" Value="Blue"/>
            </Style>
        </RichTextBox.Resources>
0
votes

In the end, I settled for the ContextMenu's resources, and this even saves me the "Style=..." in every menuItem.

<RichTextBox.ContextMenu>
    <ContextMenu>

        <ContextMenu.Resources>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="True"/>

                <Setter Property="Background" Value="{StaticResource MenuItemBackgroundBrush}"/>
            </Style>
        </ContextMenu.Resources>

    </ContextMenu>
</RichTextBox.ContextMenu>

I think I had it from the start, but somehow forgot the OverridesDefaultStyle = true which seemed to be the issue. (though from what I understood of the MS Documentation, it should not have made any difference in my case... I'll have to try and understand what I missed there)