【问题标题】:Controls outside context menu does not get hit on a single click when context menu is open上下文菜单打开时,单击上下文菜单外的控件不会被击中
【发布时间】:2018-05-07 20:30:54
【问题描述】:

我有一个按钮,点击它会打开一个上下文菜单。我在同一个堆栈面板上有另一个按钮,当通过单击按钮 2 打开上下文菜单时,单击按钮 1 不会第一次触发按钮 1 的事件,但是当再次单击时,事件会被触发。简而言之,当按钮 2 的上下文菜单打开时,我需要单击 2 次才能在我的窗口上触发按钮 1 的事件。

下面是代码,便于理解:

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

        }

【问题讨论】:

  • 发生这种情况是因为第一次单击会从打开的上下文菜单中移除焦点。处理预览鼠标按下事件并手动将焦点设置到按钮可能会有所帮助。
  • 这是预期的行为。您需要先关闭弹出窗口,然后才能单击按钮。
  • @mm8 你是对的,PopUp 必须先关闭,然后再调用事件处理程序,但它也可以一键运行。

标签: c# wpf menu popup contextmenu


【解决方案1】:

我无法解释,但是,如果你删除了

StaysOpen="False"

在 XAML 中的 ContextMenu 定义中,它将按预期工作。顺便说一句 - 您不需要在 XAML 按钮的触发器和 Click EventHandler 中调用 ContextMenu 两次。

MSDN 写StaysOpen 默认为 false。

如果菜单应该保持打开状态直到 IsOpen 属性更改为 错误的;否则为假。默认为假。

【讨论】:

  • 谢谢@Rekshino。删除 StaysOpen = "false" 允许我单击一下外部控件。但后来我观察到我的上下文菜单没有通过单击按钮关闭。因此,根据您的建议,我通过 Button 的触发器以及 ClickEventHandler 删除了对上下文菜单的重复调用。我从这里 (stackoverflow.com/questions/8958946/…) 将下拉行为添加到我的按钮。这些变化帮助我实现了我想要的。感谢您的回答,向我展示了正确的路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多