2016-01-27 154 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