如何正确设置WPF上下文菜单的样式? [英] How do I style a WPF context menu correctly?

查看:148
本文介绍了如何正确设置WPF上下文菜单的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了好几天时间尝试设置TextBox的ContextMenu样式,但无法在任何有用的解决方案附近使用。出于某种原因,在应用MenuItem的某些部分时,ContextMenu从未应用于TextBox - 请参阅链接页面上的屏幕截图。



奇怪的是ContextMenu是在主窗口文本编辑器中设置好,但不在文本框中,如下面的链接页面所示。有人知道我在这里做错了吗?



Edi - 文档 [ ^ ]



我尝试了什么:



试图改造​​风格ContextMenu(更改了背景颜色或边框颜色)看错了什么但它似乎没有效果所以我想知道这是如何正确实现的...

解决方案

< blockquote> 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 >
< span class =code-keyword>< menuitem style = {DynamicResource MetroMenuItem} command = ApplicationCommands.Copy >
< menuitem 样式 = {DynamicResource MetroMenuItem } command = ApplicationCommands.Paste >
< ; / contextmenu >





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



 < ;   style     x:key   =  MetroTextBox    targettype   =  {x:Type TextBox} < span class =code-keyword>>  
...
< Setter 属性 = ContextMenu = {DynamicResource TextBoxMetroContextMenu} / >
< / style >


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...

解决方案

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">
</contextmenu>



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}" />
</style>


这篇关于如何正确设置WPF上下文菜单的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆