仅使用 XAML 在按钮上 IsMouseOver 时调出 ContextMenu
我正在尝试使用 XAML(仅,无代码隐藏)来调出按钮的 ContextMenu。
我的按钮在这里
<Button x:Name="btn" Style="{StaticResource mybutton}" >
<Button.ContextMenu>
<ContextMenu>
<TextBlock Text="Information"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
按钮的样式在这里
<Style TargetType="{x:Type Button}" x:Key="mybutton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContextMenu.IsOpen" Value="True"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的 google-fu 令我失望,因为这似乎是一个简单的解决方案。我真的更愿意避免使用代码隐藏(MouseEnter/MouseLeave 事件)。
先感谢您。
I am trying to use XAML (only, no codebehind) to bring up the ContextMenu of a button.
I have this my button here
<Button x:Name="btn" Style="{StaticResource mybutton}" >
<Button.ContextMenu>
<ContextMenu>
<TextBlock Text="Information"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
The Style for the button here
<Style TargetType="{x:Type Button}" x:Key="mybutton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContextMenu.IsOpen" Value="True"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My google-fu is failing me for what seems like an easy solution. I really would prefer to avoid using codebehind (MouseEnter/MouseLeave events).
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试通过在“TargetName”属性中提供名称来将“Setter”应用于 ControlTemplate 中的 ContextMenu。例如:
Try to apply "Setter" for a ContextMenu within the ControlTemplate, by providing it's name in the "TargetName" property. For example:
我猜这就是你想要的 -
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/adafe007-9637-4f28-8366-8f14ead2bd75
This is what you want i guess -
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/adafe007-9637-4f28-8366-8f14ead2bd75
最好在代码隐藏中使用 mouse_up 事件。使用
MouseOver
时,用户会感到被愚弄,因为当鼠标移向某个条目时,上下文菜单消失了......在 VB 中,代码如下所示:
anyCountrol
代表发送者或保存ContextMenu
的任何其他控件It is best to use a mouse_up event in codebehind. With
MouseOver
the user feels fooled as the context menu is gone when the mouse moves towards an entry...In VB the code looks like:
anyCountrol
stands for the sender or any other control that holds theContextMenu