在 XAML 中触发 ContextMenu.IsOpen
这就是我想要做的:
<Style x:Key="TreeViewItemStyle">
<Setter Property="TreeViewItem.ContextMenu" Value="{StaticResource ContextMenu}" />
<Style.Triggers>
<Trigger Property="TreeViewItem.ContextMenu.IsOpen" Value="True">
<Setter Property="TreeViewItem.BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Yellow" GlowSize="2"/>
</Setter.Value>
</Setter>
</Trigger>
</Style>
...
但它显然不起作用,因为 Property="TreeViewItem.ContextMenu.IsOpen"
无法识别。对我需要改变什么有什么建议吗?
Here's what I'm trying to do:
<Style x:Key="TreeViewItemStyle">
<Setter Property="TreeViewItem.ContextMenu" Value="{StaticResource ContextMenu}" />
<Style.Triggers>
<Trigger Property="TreeViewItem.ContextMenu.IsOpen" Value="True">
<Setter Property="TreeViewItem.BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Yellow" GlowSize="2"/>
</Setter.Value>
</Setter>
</Trigger>
</Style>
...
But it is obviously not working because Property="TreeViewItem.ContextMenu.IsOpen"
is not recognized. Any suggestions to what I need to change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 DataTrigger 绑定到上下文菜单的 IsOpened 属性:
不幸的是,由于 TreeView 中的所有项目共享相同的 ContextMenu,这将同时突出显示所有项目。似乎没有一个属性可以让您找出哪个 FrameworkElement 打开了 ContextMenu。
您可以处理 TreeViewItem 上的 ContextMenuOpening 和 ContextMenuClosing 事件,因为这些事件将从处理单击的控件中冒出并传递到正确的 TreeViewItem。如果您想在 XAML 中执行此操作,则可以使用 EventTrigger 来启动更改属性的一帧动画。最干净的选项可能是编写一个附加行为来处理 ContextMenuOpening 和 ContextMenuClosing 事件,并在上下文菜单打开时将附加属性设置为 true。
You can bind to the IsOpened property of the context menu using a DataTrigger:
Unfortunately, since all of the items in TreeView share the same ContextMenu, that will highlight all of them at once. There doesn't seem to be a property that lets you find out which FrameworkElement opened the ContextMenu.
You could handle the ContextMenuOpening and ContextMenuClosing events on the TreeViewItem, since those will bubble up from the control that handled the click and pass through the right TreeViewItem. If you want to do it in XAML, you could use an EventTrigger to start a one-frame animation that changes your property. The cleanest option may be to write an attached behavior that handles the ContextMenuOpening and ContextMenuClosing events and sets an attached property to true when the context menu is open.