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

WPF - Button context menu not displayed properly

有一段时间我在开发 WPF 应用程序。几周前,我向按钮添加了上下文菜单。返回显示正常。

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

现在我意识到上下文菜单显示不正确。正如您在所附图片中看到的那样,左侧有一个蓝色边框区域,我确定几周前不存在。 仔细看了代码,还是想不通菜单显示错误的原因。

这是带有上下文菜单的按钮的 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>

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

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

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

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