WPF-按钮上下文菜单显示不正确

问题描述 投票:0回答:1

一段时间以来,我正在使用WPF应用程序。几周前,我已经在按钮上添加了上下文菜单。返回它正确显示。

然后,我在不更改甚至不使用上下文菜单的情况下,在应用程序的其他区域工作。

现在,我意识到上下文菜单显示不正确。如您在所附图片中所看到的,左侧有一个蓝色边框区域,我确定几周前还没有。enter image description here我已经看了很深的代码,但无法弄清楚菜单显示错误的原因。

这是带有上下文菜单的按钮的XAML:

<!-- NOTE: 1) This Button only contains a context menu and is not
              bound to a command itself. 
           2) An EventTrigger is set up to also open the context 
              menu on left click.
           3) As a ContextMenu isn't part of the VisualTree and 
              thus the menu items can't be out-of-the-box bound
              to commands a BindingProxy (custom class) is used -->
<Button Content="_Manage" Grid.Row="1" Grid.Column="1"
        IsEnabled="{Binding IsMenuAllowed}">
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
      <EventTrigger.Actions>
        <BeginStoryboard>
          <Storyboard>
            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
              <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
            </BooleanAnimationUsingKeyFrames>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>
    </EventTrigger>
  </Button.Triggers>
  <Button.ContextMenu>
    <ContextMenu>
      <MenuItem Header="_New" 
                Command="{Binding Source={StaticResource Proxy}, 
                          Path=Data.CmdNew}"/>
      <Separator/>
      <MenuItem Header="_Rename" 
                Command="{Binding Source={StaticResource Proxy}, 
                          Path=Data.CmdRename}"/>
      <MenuItem Header="_Duplicate" 
                Command="{Binding Source={StaticResource Proxy}, 
                          Path=Data.CmdDuplicate}"/>
      <Separator/>
      <MenuItem Header="Delete"  
                Command="{Binding Source={StaticResource Proxy}, 
                          Path=Data.CmdDelete}"/>
    </ContextMenu>
  </Button.ContextMenu>
</Button>

[App.xaml中设置了许多样式:

<Application.Resources>
<!-- Define the default style for GroupBox -->
<Style TargetType="{x:Type GroupBox}">
  <Setter Property="HeaderTemplate">
    <Setter.Value>
      <DataTemplate>
        <TextBlock Text="{Binding}" Height="20" />
      </DataTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!-- Define the global style for PasswordBoxes -->
<Style TargetType="{x:Type PasswordBox}">
  <Setter Property="Margin" Value="3,0,3,3"/>
  <Setter Property="MinWidth" Value="80"/>
  <Setter Property="Height" Value="24"/>
  <Setter Property="VerticalContentAlignment" Value="Center"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
</Style>

<!-- Define the global style for TextBoxes -->
<Style TargetType="{x:Type TextBox}" >
  <Setter Property="Margin" Value="3,0,3,3" />
  <Setter Property="MinWidth" Value="80"/>
  <Setter Property="Height" Value="24"/>
  <Setter Property="VerticalContentAlignment" Value="Center"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="True">
      <Setter Property="Background" Value="Red" />
      <Setter Property="ToolTip"
              Value="{Binding RelativeSource={RelativeSource Self}, 
              Path=(Validation.Errors).CurrentItem.ErrorContent}" />
    </Trigger>
  </Style.Triggers>
</Style>

<!-- Define the default style for the Separator -->
<Style TargetType="{x:Type Separator}">
  <Setter Property="Margin" Value="0,6,0,6"/>
</Style>

<!-- Define the default style for StackPanel -->
<Style TargetType="{x:Type StackPanel}">
  <Setter Property="Margin" Value="3,6,3,3"/>
</Style>

<!-- Define the default style for Button -->
<Style TargetType="{x:Type Button}">
  <Setter Property="Width" Value="70"/>
  <Setter Property="Height" Value="24"/>
  <Setter Property="Margin" Value="3,0,3,3"/>
</Style>

<!--Define a style for disabled Image Button -->
<Style x:Key="ImageEnabled" TargetType="Image">
  <Style.Triggers>
    <Trigger Property="IsEnabled" Value="False">
      <Setter Property="Opacity" Value="0.25" />
    </Trigger>
  </Style.Triggers>
</Style>

<!-- Define the default style for ComboBoxes -->
<Style TargetType="{x:Type ComboBox}">
  <Setter Property="MinWidth" Value="80"/>
  <Setter Property="Height" Value="24"/>
  <Setter Property="Margin" Value="3,0,3,3"/>
</Style>

<!-- Define the default style for Label -->
<Style TargetType="{x:Type Label}">
  <Setter Property="Margin" Value="0,3,3,0" />
</Style>


<!-- Define the default style for CheckBox -->
<Style TargetType="{x:Type CheckBox}">
  <Setter Property="Margin" Value="3,6,3,3" />
</Style>

<!-- Define the default style for Rectangles (Canvas drawing) -->
<Style TargetType="{x:Type Rectangle}">
  <Setter Property="Stroke" Value="#6080bc"/>
  <Setter Property="StrokeThickness" Value="1"/>
</Style>

<!-- Define the default style for Lines (Canvas drawing) -->
<Style TargetType="{x:Type Line}">
  <Setter Property="Stroke" Value="#6080bc"/>
  <Setter Property="StrokeThickness" Value="1"/>
</Style>

<!-- Define the default style for Path (Canvas drawing) -->
<Style TargetType="{x:Type Path}">
  <Setter Property="Stroke" Value="#6080bc"/>
  <Setter Property="StrokeThickness" Value="1"/>
</Style>
</Application.Resources>

有人知道这个问题的原因是什么吗?

c# wpf xaml contextmenu
1个回答
0
投票

我已经复制了您的代码,并能够在上下文菜单上复制蓝色边框区域。

通过注释掉以矩形为目标的样式,将删除蓝色边框区域。如果您在程序的其他位置使用此样式,则可以尝试使用x:Key值定位需要边框的特定矩形。

x:Type属性会影响所有矩形,我猜是逻辑xaml树中的某个上下文菜单包含一个矩形。

© www.soinside.com 2019 - 2024. All rights reserved.