WPF:如何为上下文菜单实现Blend的Dark主题

时间:2013-01-14 15:42:20

标签: wpf contextmenu

我在我的App.xaml中添加了这个xaml:

<Style TargetType="{x:Type ContextMenu}">
   <Setter Property="Background" Value="{StaticResource ShadeBrush}" />
   <Setter Property="BorderBrush" Value="{StaticResource ShadeBrush}" />
   <Setter Property="Foreground" Value="White" />
</Style>    

这让我走向黑暗主题的大部分方向...... 如何使用菜单项解决此问题:
enter image description here

我怀疑我需要修改menuitem的样式或模板 感谢。

更新:使用Snoop(Andy提到,谢谢),我在选择白色矩形时发现这一点:
enter image description here

1 个答案:

答案 0 :(得分:4)

对于接下来来到这里的人,我通过设置模板解决了这个问题:

<Style TargetType="{x:Type ContextMenu}">
    <Setter Property="Background" Value="{StaticResource ShadeBrush}" />
    <Setter Property="BorderBrush" Value="{StaticResource ShadeBrush}" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContextMenu}">
                <Border Uid="Border_93">
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="Tag" 
                    Value="{DynamicResource 
                        {x:Static SystemParameters.DropShadowKey}}"/>
                            <Style.Triggers>
                                <DataTrigger 
                            Binding="{Binding Tag, 
                                RelativeSource={RelativeSource Self}}" 
                            Value="True">
                                    <Setter Property="Background" 
                            Value="Transparent"/>
                                    <Setter Property="Padding" 
                            Value="0,0,5,5"/>
                                    <Setter Property="Effect">
                                        <Setter.Value>
                                            <DropShadowEffect 
                                    BlurRadius="4" 
                                    Opacity="0.8" 
                                    ShadowDepth="1"/>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}" 
                Background="{TemplateBinding Background}" 
                Uid="Border_50">
                        <ScrollViewer CanContentScroll="True" 
                Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, 
                        TypeInTargetAssembly={x:Type FrameworkElement}}}" 
                        Uid="ScrollViewer_9">
                            <ItemsPresenter 
                    KeyboardNavigation.DirectionalNavigation="Cycle" 
                    Margin="{TemplateBinding Padding}" 
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                    Uid="ItemsPresenter_5"/>
                        </ScrollViewer>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>