【问题标题】:wpf MenuItem dropdown background colorwpf MenuItem下拉背景颜色
【发布时间】:2018-12-14 12:32:26
【问题描述】:

我想更改 MenuItems 下拉菜单的背景颜色,我发现我可以做到,如果我更改 ControlTemplate "x:Static MenuItem.TopLevelHeaderTemplateKey"。

我复制了 controlTemplate来自Here 并编辑所有颜色,但现在它说“StaticResource MenuScrollViewer”是未知的。我也从同一个站点添加了此资源,但在 MenuScrollView 中缺少许多其他资源。

那么如何编辑此模板,以便更改“MenuItem.TopLevelHeaderTemplateKey”的所有颜色?

【问题讨论】:

  • 您需要包含所有引用的资源。
  • 还是不行。 “MenuScrollButton”不见了
  • 如果缺少“MenuScrollButton”,您显然没有包含此资源,是吗?
  • 但它不是页面。我在哪里可以找到它?
  • 在您找到引用它的资源的同一个地方。

标签: c# wpf templates menuitem


【解决方案1】:

您会在C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.* 程序集中找到默认模板以及所有引用的资源。

下载dotPeek或使用其他一些.NET反编译器来反编译它们查看Resources->PresentationFramework.*.g.resources->themes下的BAML资源。

【讨论】:

    【解决方案2】:

    模板在您链接的页面中。或者至少是英文版。 这是菜单滚动按钮:

        <Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}">
            <Setter Property="ClickMode" Value="Hover"/>
            <Setter Property="MinWidth" Value="0"/>
            <Setter Property="MinHeight" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <DockPanel Background="Transparent" SnapsToDevicePixels="true">
                            <Rectangle x:Name="R1" DockPanel.Dock="Right" Fill="Transparent" Width="1"/>
                            <Rectangle x:Name="B1" DockPanel.Dock="Bottom" Fill="Transparent" Height="1"/>
                            <Rectangle x:Name="L1" DockPanel.Dock="Left" Fill="Transparent" Width="1"/>
                            <Rectangle x:Name="T1" DockPanel.Dock="Top" Fill="Transparent" Height="1"/>
                            <ContentPresenter x:Name="ContentContainer" HorizontalAlignment="Center" Margin="2,2,2,2" VerticalAlignment="Center"/>
                        </DockPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Fill" TargetName="R1" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
                                <Setter Property="Fill" TargetName="B1" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
                                <Setter Property="Fill" TargetName="L1" Value="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"/>
                                <Setter Property="Fill" TargetName="T1" Value="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"/>
                                <Setter Property="Margin" TargetName="ContentContainer" Value="3,3,1,1"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    对于整个提取的模板,请参阅:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/41ad0250-d0f9-4a24-b668-35020f0cfdf9/wpf-menu-customization?forum=wpf

    【讨论】:

    • 此代码在我的链接页面上。主要问题是缺少 MenuScrollButton。
    • 我添加了这个和一个帖子的链接,该链接解释了如何提取模板,看起来它包含所有内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多