Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 上下文菜单的自定义菜单项样式?_C#_.net_Wpf_Controltemplate - Fatal编程技术网

C# 上下文菜单的自定义菜单项样式?

C# 上下文菜单的自定义菜单项样式?,c#,.net,wpf,controltemplate,C#,.net,Wpf,Controltemplate,我需要为MenuItem创建一个自定义样式,每个MenuItem角色都有一组ControlTemplate。所以,我决定使用 但是,有一个问题-我想在弹出窗口中获得相同的外观。 不幸的是,外观与下图不同: 代码示例: <Window x:Class="ExporerSubMenu.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="ht

我需要为
MenuItem
创建一个自定义样式,每个
MenuItem
角色都有一组
ControlTemplate
。所以,我决定使用

但是,有一个问题-我想在
弹出窗口中获得相同的外观。
不幸的是,外观与下图不同:

代码示例:

<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>

现在,我只有一个解决方案-绘制矩形(从混合)


还有别的解决办法吗

<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>