在 WPF 中关闭上下文菜单

Close Context Menu in WPF

我已将上下文菜单添加到 WPF 中的边框。

<Border>
  <Border.ContextMenu>
    <ContextMenu x:Name="HistoryPanelContextMenu">
      <ContextMenu.Template>
        <ControlTemplate>
          <Grid Background="{Binding Background}">
            <Grid.ColumnDefinitions>
              <ColumnDefinition></ColumnDefinition>
              <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Background="Transparent" BorderBrush="Transparent" Name="CancelBtn" Content="{x:Static strings:Resource.CancelBtn}" PreviewMouseUp="CancelBtn_OnPreviewMouseUp" Foreground="#fff" FontFamily="Segoe UI Semibold" FontSize="10">
              <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                  <Border x:Name="bdr_main" Background="Transparent" Height="36" VerticalAlignment="Top" BorderBrush="#c0b6d1" BorderThickness="2" CornerRadius="2" Margin="30,0,15,0">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
                  </Border>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                      <Setter TargetName="bdr_main" Property="Background" Value="Transparent"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                      <Setter TargetName="bdr_main" Property="Background" Value="#7FC0B6D1"/>
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Button.Template>
            </Button>
            <Button Grid.Column="1" VerticalAlignment="Top" Foreground="#fff" Background="#FF567E94"  FontSize="10" Tag="{Binding Id}" PreviewMouseUp="UIElement_OnPreviewMouseUp" Margin="15,5,0,5" FontFamily="Segoe UI Semibold" >
              <TextBlock VerticalAlignment="Center">
                <Image Height="14" Width="14" Source="/Size.WPF;component/Assets/icon-trash-white.png" Margin="0,0,0,0"/>
                <TextBlock Name="DeleteBtnText" Text="{x:Static strings:Resource.DeleteBtnText}"/>
              </TextBlock>
              <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                  <Border x:Name="bdr_main" Height="36" Background="#FF567E94" BorderBrush="#FF567E94" BorderThickness="0" CornerRadius="2">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
                  </Border>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                      <Setter TargetName="bdr_main" Property="Background" Value="#FF567E94"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                      <Setter TargetName="bdr_main" Property="Background" Value="#6596b1"/>
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Button.Template>
            </Button>
          </Grid>
        </ControlTemplate>
      </ContextMenu.Template>
    </ContextMenu>
  </Border.ContextMenu>
</Border>

我试图在单击按钮时关闭此上下文菜单。我找到了将 IsOpen 设置为 false 的解决方案。好吧,我尝试了下一种方式:

HistoryPanelContextMenu.IsOpen = false;

HistoryPanelContextMenu 未定义,我不知道为什么。

那么如何在单击时关闭上下文菜单?

谢谢。

您是否尝试绑定到上下文菜单上的鼠标事件?

<ContextMenu x:Name="TimeCardGridContextMenu" MouseLeftButtonUp="DoSomething">

并使用发件人或事件参数来访问您需要的东西?