1

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)
        {

        }
DotNetSpartan
  • 931
  • 4
  • 20
  • 41
  • 1
    This is happening because the first click removes the focus from the context menu which is open. Handling the preview mouse down event and manually setting focus to the button might help. – Raviraj Palvankar May 04 '18 at 07:37
  • This is the expected behaviour. You need to close the Popup before you can click the Button. – mm8 May 04 '18 at 14:41
  • @mm8 You are right, PopUp has to be closed first and then event handler has to be called, but it works also with one click. – Rekshino May 07 '18 at 12:33

1 Answers1

1

I can't explain it, but, if you delete

StaysOpen="False"

in your ContextMenu definition in XAML, then it will work as you have it expected. By the way - you don't need to call ContextMenu twice in XAML button's triggers and in Click EventHandler.

MSDN writes StaysOpen is false by default.

true if the menu should stay open until the IsOpen property changes to false; otherwise, false. The default is false.

Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • Thanks @Rekshino. Removing StaysOpen = "false" allowed me to hit the outside controls in a single click. But then I observed that my context menu was not getting closed by clicking on the button. So, on your suggestion, I removed the repetitive calls to Context menu through Button's trigger and also from ClickEventHandler. I added the Dropdown behaviour to my button from here(https://stackoverflow.com/questions/8958946/how-to-open-a-popup-menu-when-a-button-is-clicked). These changes helped me to achieve what i was looking for. Thanks for your answer that showed me the correct path. – DotNetSpartan May 08 '18 at 09:23