2016-01-27 115 views
0

我已经将上下文菜单添加到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是不确定的,我不知道为什么。

那么我如何关闭点击上下文菜单?

谢谢。

+0

我不知道我明白你想要做什么。当你点击一个菜单项时,你的上下文菜单不会自动关闭吗? – MetalMikester

+0

@MetalMikester,好吧,也许它取决于我的上下文菜单,导致我定制它。我会添加所有我的上下文菜单模板 – demo

回答

1

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

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

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

+0

现在这有助于 – demo