【问题标题】:how to create style for default wpf contextmenu?如何为默认 wpf 上下文菜单创建样式?
【发布时间】:2013-07-04 07:42:17
【问题描述】:

我需要在哪里为上下文菜单创建一个可以自动应用的样式,我已经尝试了我在网上找到的每个示例,但没有任何效果。我已经尝试过 MSDN 链接中叙述的风格:http://msdn.microsoft.com/en-us/library/ms744758(v=vs.85).aspx

我使用了以下样式,但它不起作用。

<Style x:Key="CStyle" TargetType="ContextMenu">
        <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"
                            Background="Red"
                            BorderBrush="{StaticResource SolidBorderBrush}"
                            BorderThickness="1">
                        <StackPanel IsItemsHost="True"
                                    KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow"
                                 Value="true">
                            <Setter TargetName="Border"
                                    Property="Padding"
                                    Value="0,3,0,3" />
                            <Setter TargetName="Border"
                                    Property="CornerRadius"
                                    Value="4" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

谁能指导我实现这一目标?

我尝试将此样式应用于 TextBoxStyle(请参见下面的示例),当我运行它并右键单击文本框的内容时,我看到以下错误:

“'System.Windows.Style' 不是属性 'ContextMenu' 的有效值。” 我在以下风格的任何地方都做错了吗?请指导我。

示例文本框样式:

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseTextStyle}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="ContextMenu" Value="{StaticResource CStyle}" />
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>

【问题讨论】:

  • 这种风格适合我。你在哪里定义的?
  • 在资源部分下的 App.xaml 中定义它。我想我会工作
  • 我在 TextBoxStyles.xaml 中添加了它,它是一个资源字典。但它不工作

标签: wpf textbox contextmenu controltemplates


【解决方案1】:

编辑:基于修改后的问题

您正在尝试将ContextMenu 的值设置为Style。将您的样式更改为以下内容:

    <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseTextStyle}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu Style="{StaticResource CStyle}">
                    <MenuItem Header="Cut" Command="Cut"/>
                    <MenuItem Header="Copy" Command="Copy"/>
                    <MenuItem Header="Paste" Command="Paste"/>
                </ContextMenu>
            </Setter.Value>
        </Setter>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    </Style>

【讨论】:

  • 我有一个名为 Theme.xaml 的资源字典,其中我合并了所有资源(包括 TextBoxStyle.xaml)字典,并且 Theme.xaml 被合并到 App.xaml 中。
  • 问题内容已更改,请检查一次,提前谢谢。
  • 我已根据您的更改修改了答案。
  • 现在错误不会出现,但我无法在上下文菜单上看到默认的剪切、复制、粘贴选项,但上下文菜单上会出现垂直滚动。
  • 我认为这个解决方案行不通,TextBox 似乎忽略了任何未在本地设置的 ContextMenu 值,因此将其设置为 Style 肯定对我不起作用。
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多