C# Wpf:如何设置菜单项中项目的背景颜色

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

我想在菜单项中设置菜单项的背景颜色This 我的上下文菜单有一个style,但它显然不会改变它的颜色

我尝试更改菜单项背景,但这并没有改变边框背景,它看起来像this

我尝试创建一个style来改变边框,但它使整个事情像This

我的上下文菜单的样式:

    <Style TargetType="{x:Type ContextMenu}" x:Key="conte">
        <Setter Property="Background" Value="{DynamicResource PrimaryDark}" />
        <Setter Property="Foreground" Value="{DynamicResource Secendery}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContextMenu}">
                    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}" BorderThickness="0.5" CornerRadius="5">
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle">
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我尝试将样式设置为 itemcontainerstyle :

    <Style TargetType="{x:Type MenuItem}" x:Key="conteItem">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="BorderBrush" Value="{DynamicResource Secendery}" />
        <Setter Property="Padding" Value="5,5,5,5" />
        <Setter Property="Foreground" Value="{DynamicResource PrimaryDark}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border x:Name="Border" BorderBrush="{DynamicResource Secendery}" CornerRadius="5" Background="{DynamicResource PrimaryDark}">
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle">
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
c# wpf xaml
1个回答
0
投票

您已经接近您的

MenuItem
风格,但您需要指定该模板的正确部分。特别是,
<Popup x:Name="PART_Popup" ...>
标签指定当您单击
MenuItem
查看其子级时所获得的弹出菜单。但是,您还需要包含要显示的其他部分,例如
x:Name="Icon"
x:Name="content"
等...

下面的这个例子大量借鉴了这个答案,它能够实现我认为你正在寻找的东西

<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Grid SnapsToDevicePixels="true">
                    <Border BorderBrush="Cyan" BorderThickness="1">

                        <DockPanel>
                            <ContentPresenter x:Name="Icon" ContentSource="Icon" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" />
                            <Path x:Name="GlyphPanel" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="7,0,0,0" Visibility="Collapsed" VerticalAlignment="Center" />
                            <ContentPresenter x:Name="content" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </DockPanel>
                    </Border>
                    <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="1" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" VerticalOffset="-1">

                        <Border BorderThickness="2" BorderBrush="Black" Background="Black">
                            <ScrollViewer x:Name="SubMenuScrollViewer" CanContentScroll="true" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle" />
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="TextBlock.Foreground" Value="LightCyan" TargetName="content" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

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