Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have literally spent days trying to style the ContextMenu of a TextBox but cannot get anywhere near a useful solution. For some reason is the ContextMenu never applied to a TextBox while some parts of the MenuItem are applied - see screenshots on linked page.

The strange thing is that the ContextMenu is styled OK in the Main Window Text Editor but not in the textbox as shown in the linked page below. Does someone have an idea what I am doing wrong here?

Edi - Documentation[^]

What I have tried:

Tried to re-style the ContextMenu (changed background color or Border Color) to see whats wrong but it does not seem to have an effect so I am wondering how this is implemented correctly...
Updated 12-Oct-16 7:47am
PureNsanity 12-Oct-16 9:55am    
There are a lot of potential reasons why a WPF style won't get applied as expected. Any changes to style applied directly to the element will have highest precedence, so if you're not seeing that happen I'd start there. Could you provide a snippet with the TextBox and ContextMenu XAML?
Dirk Bahle 12-Oct-16 13:42pm    
I don't need to know a lot of reasons I need to know a way to solve the problem described. So far, I've looked only here;

and found that the dark context menu is not working there either.

Its working here:

...and I found that it is working because the TextBox is restyled with the context menu property:

So, thats how it works for me too.
PureNsanity 12-Oct-16 13:45pm    
I'm letting you know without the information provided it's impossible to tell - you would only be guessing. I'm not trying to tell you about all the ways, just saying I need to see the requested information or I won't be able to tell you.

1 solution

MahApps.Metro/Controls.ContextMenu.xaml at develop · MahApps/MahApps.Metro · GitHub[^]

<contextmenu x:key="TextBoxMetroContextMenu" style="{StaticResource MetroContextMenu}">
    <menuitem style="{DynamicResource MetroMenuItem}" command="ApplicationCommands.Cut">
    <menuitem style="{DynamicResource MetroMenuItem}" command="ApplicationCommands.Copy">
    <menuitem style="{DynamicResource MetroMenuItem}" command="ApplicationCommands.Paste">

MahApps.Metro/Controls.TextBox.xaml at develop · MahApps/MahApps.Metro · GitHub[^]

<style x:key="MetroTextBox" targettype="{x:Type TextBox}">
  <Setter Property="ContextMenu" Value="{DynamicResource TextBoxMetroContextMenu}" />
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900