[英]WPF left-click ContextMenu doesn't dissaepear on second click
I've implemented a new behaviour for WPF button for using context menu with left click: 我为WPF按钮实现了一种新行为,以通过单击鼠标左键使用上下文菜单:
public class LeftClickContextMenuButtonBehavior : Behavior<Button>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(AssociatedObject_MouseDown), true);
}
void AssociatedObject_MouseDown(object sender, RoutedEventArgs e)
{
Button source = sender as Button;
if (source != null && source.ContextMenu != null)
{
source.ContextMenu.PlacementTarget = source;
source.ContextMenu.Placement = PlacementMode.Bottom;
source.ContextMenu.IsOpen = !source.ContextMenu.IsOpen;
}
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.RemoveHandler(UIElement.MouseDownEvent, new RoutedEventHandler(AssociatedObject_MouseDown));
}
}
XAML: XAML:
<Button Content="Left ContextMenu test">
<i:Interaction.Behaviors>
<extensions:LeftClickContextMenuButtonBehavior />
</i:Interaction.Behaviors>
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Item A" />
<MenuItem Header="Item B" />
</ContextMenu>
</Button.ContextMenu>
</Button>
It works fine, but I have a small problem - on a second click on the button (during the context menu is still opened), menu closes and immediately re-opens, but expected behaviour is to close the menu - source.ContextMenu.IsOpen = !source.ContextMenu.IsOpen;
它工作正常,但我有一个小问题-再次单击按钮(在上下文菜单仍处于打开状态),菜单关闭并立即重新打开,但是预期的行为是关闭菜单
source.ContextMenu.IsOpen = !source.ContextMenu.IsOpen;
. 。 So it seems that before MoseDown on button is fired, some other fuctionality closes the menu.
因此,似乎在触发MoseDown on按钮之前,其他一些功能会关闭菜单。 How to avoid that?
如何避免呢?
I think i've found a solution: 我想我已经找到了解决方案:
<Button Content="Left ContextMenu test" IsHitTestVisible="{Binding ElementName=cm, Path=IsOpen, Mode=OneWay, Converter={StaticResource BoolInverter}}">
<i:Interaction.Behaviors>
<extensions:LeftClickContextMenuButtonBehavior />
</i:Interaction.Behaviors>
<Button.ContextMenu>
<ContextMenu x:Name="cm">
<MenuItem Header="Item A" />
<MenuItem Header="Item B" />
</ContextMenu>
</Button.ContextMenu>
</Button>
Where the BoolInverterConverter is defined as: 其中BoolInverterConverter定义为:
public class BoolInverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
return !(bool)value;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
In this way when you click the button for the second time is not clicked but the context menu will close because it lost focus. 这样,当您第二次单击该按钮时,不会被单击,但是上下文菜单将关闭,因为它失去了焦点。
Try this: 尝试这个:
void AssociatedObject_MouseDown(object sender, RoutedEventArgs e)
{
e.handled = true; // handle the event
Button source = sender as Button;
//rest of the code ...
}
Best of luck ! 祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.