C# 在WPF中设置多个菜单的样式

C# 在WPF中设置多个菜单的样式,c#,wpf,xaml,C#,Wpf,Xaml,我是WPF的新手,正在尝试了解如何为Windows7触摸屏应用程序定制菜单。我正在使用下面的xaml,它是从StackOverflow的另一个问题中提取的,用于设置其中一个菜单的样式。我现在想设计另一个菜单,它将以不同的方式使用。我如何设计另一个相同类型的菜单 如果答案应该是简单的/我应该能够弄明白的,请发布一个链接到我可以阅读如何做这类事情的地方。我已经回顾了MSDN一段时间了,虽然我阅读了所有我已经知道的东西,但在这里我看不到任何对我有帮助的东西。(很抱歉,我一直在努力寻找完成一项简单任务所

我是WPF的新手,正在尝试了解如何为Windows7触摸屏应用程序定制菜单。我正在使用下面的xaml,它是从StackOverflow的另一个问题中提取的,用于设置其中一个菜单的样式。我现在想设计另一个菜单,它将以不同的方式使用。我如何设计另一个相同类型的菜单

如果答案应该是简单的/我应该能够弄明白的,请发布一个链接到我可以阅读如何做这类事情的地方。我已经回顾了MSDN一段时间了,虽然我阅读了所有我已经知道的东西,但在这里我看不到任何对我有帮助的东西。(很抱歉,我一直在努力寻找完成一项简单任务所需的基本信息,这让我很沮丧。)


为您的样式指定不同的键:

<Style TargetType="ContextMenu" x:Key="MyStyle1">
</Style>

<Style TargetType="ContextMenu" x:Key="MyStyle2">
</Style>

然后需要指定上下文菜单要使用的样式

<ContextMenu Style="{StaticResource MyStyle1}"></ContextMenu>

<ContextMenu Style="{StaticResource MyStyle2}"></ContextMenu>

如果这些上下文菜单有任何共享样式,可以执行以下操作:

<Style TargetType="ContextMenu" x:Key="BaseStyle"></Style

<Style TargetType="ContextMenu" x:Key="MyStyle1" BasedOn="{StaticResource BaseStyle}">
</Style>

<Style TargetType="ContextMenu" x:Key="MyStyle2" BasedOn="{StaticResource BaseStyle}">
</Style>

我最终在styles.resources中定义了子对象的样式

    <Style TargetType="ContextMenu" x:Key="UserMenu">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
        <Setter Property="HasDropShadow" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContextMenu">
                    <Border 
                            Name="Border"
                            BorderThickness="1"
                            BorderBrush="{DynamicResource userContextMenuBorder}"
                            Background="{DynamicResource userContextMenuBackground}"
                            MinWidth="182"
                            MinHeight="60"
                            >

                        <StackPanel IsItemsHost="True"
                                KeyboardNavigation.DirectionalNavigation="Cycle"
                                >
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow" Value="true">
                            <Setter TargetName="Border" Property="Padding" Value="0,3,0,3"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Resources>
            <!-- SimpleStyles: MenuItem -->

            <Style TargetType="{x:Type Separator}">
                <Setter Property="Height" Value="1"/>
                <Setter Property="Margin" Value="0,4,0,4"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Separator}">
                            <Border BorderBrush="{DynamicResource userContextMenuSeparatorBorder}" BorderThickness="1"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style>


不同的上下文菜单是否需要两种不同的样式?是的。上下文菜单将根据使用位置的不同而有所不同。因此,对于已经使用键的当前样式,我将如何实现这一点:IE
{x:Static MenuItem.SeparatorStyleKey}
为了拥有多个样式,您需要有多个键。因此,您仍然可以对其中一种样式使用静态键,但要有另一种样式,您需要定义一个新键,如答案中的示例。但是,对于这些备用样式,您需要手动将样式分配给要设置不同样式的每个项目…我无法手动分配这些样式,因为菜单项是动态创建的。您可以为不同的菜单设置不同的样式,对吗?对于每个菜单,您可以使用不同的ItemTemplate创建一个样式。如果还不清楚,我可能需要查看xaml的其余部分(不仅仅是Window.Resources)
    <Style TargetType="ContextMenu" x:Key="UserMenu">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
        <Setter Property="HasDropShadow" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContextMenu">
                    <Border 
                            Name="Border"
                            BorderThickness="1"
                            BorderBrush="{DynamicResource userContextMenuBorder}"
                            Background="{DynamicResource userContextMenuBackground}"
                            MinWidth="182"
                            MinHeight="60"
                            >

                        <StackPanel IsItemsHost="True"
                                KeyboardNavigation.DirectionalNavigation="Cycle"
                                >
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow" Value="true">
                            <Setter TargetName="Border" Property="Padding" Value="0,3,0,3"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Resources>
            <!-- SimpleStyles: MenuItem -->

            <Style TargetType="{x:Type Separator}">
                <Setter Property="Height" Value="1"/>
                <Setter Property="Margin" Value="0,4,0,4"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Separator}">
                            <Border BorderBrush="{DynamicResource userContextMenuSeparatorBorder}" BorderThickness="1"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style>