wpf MenuItem下拉背景颜色

问题描述 投票:0回答:2

我想更改MenuItems下拉列表的背景颜色,我发现我可以创建它,如果我更改ControlTemplate“x:Static MenuItem.TopLevelHeaderTemplateKey”。 我从Here复制了controlTemplate并编辑了所有颜色但是现在它说“StaticResource MenuScrollViewer”是未知的。我也从同一站点添加了此资源,但MenuScrollView中缺少许多其他资源。 那么如何编辑此模板以便我可以更改“MenuItem.TopLevelHeaderTemplateKey”的所有颜色?

c# wpf templates menuitem
2个回答
0
投票

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

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


0
投票

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

    <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

© www.soinside.com 2019 - 2024. All rights reserved.