在 XAML 中触发 ContextMenu.IsOpen

发布于 2024-09-06 10:12:47 字数 718 浏览 3 评论 0原文

这就是我想要做的:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-09-13 10:12:48

您可以使用 DataTrigger 绑定到上下文菜单的 IsOpened 属性:

<DataTrigger Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" Value="True">
    <Setter Property="Background" Value="Green"/>
</DataTrigger>

不幸的是,由于 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:

<DataTrigger Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" Value="True">
    <Setter Property="Background" Value="Green"/>
</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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文