title: Close Context Menu in WPF
body: <p>I've add context menu to border in WPF.</p>
  
  <pre><code>&lt;Border&gt;
    &lt;Border.ContextMenu&gt;
      &lt;ContextMenu x:Name="HistoryPanelContextMenu"&gt;
        &lt;ContextMenu.Template&gt;
          &lt;ControlTemplate&gt;
            &lt;Grid Background="{Binding Background}"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
                &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;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"&gt;
                &lt;Button.Template&gt;
                  &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                    &lt;Border x:Name="bdr_main" Background="Transparent" Height="36" VerticalAlignment="Top" BorderBrush="#c0b6d1" BorderThickness="2" CornerRadius="2" Margin="30,0,15,0"&gt;
                      &lt;ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" /&gt;
                    &lt;/Border&gt;
                    &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                        &lt;Setter TargetName="bdr_main" Property="Background" Value="Transparent"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsPressed" Value="True"&gt;
                        &lt;Setter TargetName="bdr_main" Property="Background" Value="#7FC0B6D1"/&gt;
                      &lt;/Trigger&gt;
                    &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
                &lt;/Button.Template&gt;
              &lt;/Button&gt;
              &lt;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" &gt;
                &lt;TextBlock VerticalAlignment="Center"&gt;
                  &lt;Image Height="14" Width="14" Source="/Size.WPF;component/Assets/icon-trash-white.png" Margin="0,0,0,0"/&gt;
                  &lt;TextBlock Name="DeleteBtnText" Text="{x:Static strings:Resource.DeleteBtnText}"/&gt;
                &lt;/TextBlock&gt;
                &lt;Button.Template&gt;
                  &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                    &lt;Border x:Name="bdr_main" Height="36" Background="#FF567E94" BorderBrush="#FF567E94" BorderThickness="0" CornerRadius="2"&gt;
                      &lt;ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" /&gt;
                    &lt;/Border&gt;
                    &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                        &lt;Setter TargetName="bdr_main" Property="Background" Value="#FF567E94"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsPressed" Value="True"&gt;
                        &lt;Setter TargetName="bdr_main" Property="Background" Value="#6596b1"/&gt;
                      &lt;/Trigger&gt;
                    &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
                &lt;/Button.Template&gt;
              &lt;/Button&gt;
            &lt;/Grid&gt;
          &lt;/ControlTemplate&gt;
        &lt;/ContextMenu.Template&gt;
      &lt;/ContextMenu&gt;
    &lt;/Border.ContextMenu&gt;
  &lt;/Border&gt;
  </code></pre>
  
  <p>I tried to close this context menu on button click. I found solution just to set <code>IsOpen</code> to <code>false</code>. Well, I tried it in next way :</p>
  
  <pre><code>HistoryPanelContextMenu.IsOpen = false;
  </code></pre>
  
  <p>but <code>HistoryPanelContextMenu</code> is undefined and I don't know why.</p>
  
  <p>So How I can close context menu on click?</p>
  
  <p>Thanks.</p>