ContextMenu的自定义MenuItem样式?

时间:2017-01-17 14:48:48

标签: c# .net wpf controltemplate

我需要为MenuItem创建一个自定义样式,每个ControlTemplate MenuItem设置一组Role。所以,我决定使用this approach from msdn

但是,有一个问题 - 我希望在Popup中获得相同的外观。 不幸的是,外观与下图不同:

enter image description here

代码示例:

<Window x:Class="ExporerSubMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExporerSubMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="1000">

    <Window.Resources>

    <!-- Copy all styles for MenuItem ControlTemplate and Brushes -->
    <!-- https://msdn.microsoft.com/fr-fr/library/ms747082(v=vs.85).aspx -->

    </Window.Resources>

    <Grid Background="LightBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBox Text="Local (right click)" FontSize="16" Grid.Column="0">
            <TextBox.ContextMenu>
                <ContextMenu>

                    <MenuItem Header="Restore...">
                        <MenuItem Header="to desktop" />
                        <MenuItem Header="to another folder" />
                        <MenuItem Header="db schema and data..." />
                    </MenuItem>

                    <MenuItem Header="to .bac file">
                        <MenuItem Header="to desktop" />
                        <MenuItem Header="to another folder" />
                    </MenuItem>

                    <MenuItem Header="to db files">
                        <MenuItem Header="to desktop" />
                        <MenuItem Header="to another folder" />
                    </MenuItem>

                    <MenuItem Header="Export data">
                        <MenuItem Header="db schema to desktop" />
                        <MenuItem Header="db schema and data..." />
                    </MenuItem>

                    <MenuItem Header="Remove" />
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>

    </Grid>
</Window>

现在,我只有一个解决方案 - 绘制矩形(来自Blend)

<Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" HorizontalOffset="-2" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Right" VerticalOffset="-3">
    <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
        <Border x:Name="SubMenuBorder" BorderBrush="#FF959595" BorderThickness="1" Background="WhiteSmoke">
            <ScrollViewer x:Name="SubMenuScrollViewer" Margin="1,0" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                <Grid x:Name="Grid2" RenderOptions.ClearTypeHint="Enabled">
                    <Canvas x:Name="Canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                        <Rectangle x:Name="OpaqueRect" Fill="WhiteSmoke" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                    </Canvas>
                    <Rectangle x:Name="Rectangle" Fill="#FFF1F1F1" HorizontalAlignment="Left" Margin="1,2" RadiusY="2" RadiusX="2" Width="28"/>
                    <Rectangle x:Name="Rectangle1" Fill="#FFE2E3E3" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>
                    <Rectangle x:Name="Rectangle2" Fill="White" HorizontalAlignment="Left" Margin="30,2,0,2" Width="1"/>
                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                </Grid>
            </ScrollViewer>
        </Border>
    </Themes:SystemDropShadowChrome>
</Popup>

还有其他解决方法可以解决这个问题吗?

0 个答案:

没有答案