I've a button, clicking on it opens a context menu. I've another button on the same stackpanel, When the context menu is open through button 2 click, Clicking on button 1 does not fires the event of button 1 for the first time, But when one more click is made the event is fired. In simple words, It takes me 2 clicks to fire the event of button 1 on my window when the context menu of button 2 is open.
Here's the code for more understanding:
MainWindow.xaml
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row = "0" Orientation="Horizontal">
<Button x:Name= "btnOne" Click="btnOneClick"/>
<Button x:Name="btnTwo"
RenderOptions.BitmapScalingMode="HighQuality" Cursor="Hand" ContextMenuService.IsEnabled="False" Click="btnTwoClick">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="MyContextMenu"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
<StackPanel>
<StackPanel Height="{Binding ActualHeight, ElementName=btnTwo}" Orientation="Horizontal" HorizontalAlignment="Right" >
<TextBlock HorizontalAlignment="Right" Text="Click Me" ></TextBlock>
</StackPanel>
</StackPanel>
<Button.ContextMenu>
<ContextMenu x:Name="MyContextMenu" StaysOpen="False" Width="200" MaxHeight="258"
BorderBrush="Gray" BorderThickness="1,0,1,1" Closed="MyContextMenuClosed">
<MenuItem Header="MenuOne" StaysOpenOnClick="True" Cursor="Arrow"/>
<MenuItem Header="MenuTwo" StaysOpenOnClick="True" Cursor="Arrow"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</Grid>
MainWindow.xaml.cs
//When user clicks anywhere on the Grid
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//Close the Context menu if it's open
if (btnTwo.ContextMenu.IsOpen)
btnTwo.ContextMenu.IsOpen = false;
}
//When user clicks on btnTwo
private void btnTwoClick(object sender, RoutedEventArgs e)
{
if (!btnTwo.ContextMenu.IsOpen)
{
//Open the Context menu when Button is clicked
btnTwo.ContextMenu.IsEnabled = true;
btnTwo.ContextMenu.PlacementTarget = (sender as Button);
btnTwo.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
btnTwo.ContextMenu.IsOpen = true;
}
}
//When user clicks on btnOne, This event does does not fires when context menu is open
private void btnOneClick(object sender, RoutedEventArgs e)
{
}