简体   繁体   English

WPF:突出显示TreeView项目显示上下文

[英]WPF: Highlight TreeView Item Displaying Context Meu

I am using a TreeView for filters selection. 我正在使用TreeView进行过滤器选择。

Different categories on the TreeView start different filters, that may take up to 3 seconds to run. TreeView上的不同类别会启动不同的筛选器,最多可能需要3秒钟才能运行。

Each category has it's own context menu, where u can rename, delete, etc. 每个类别都有其自己的上下文菜单,您可以在其中重命名,删除等。

I want to make it visible to the user on which item he rightclicked. 我想让用户可以看到他右键单击的项目。 On way would be to select the item on the rightclick event. 可以选择右键单击事件上的项目。 But this causes the menu to load slowly, since it runs the related filter. 但这会导致菜单加载缓慢,因为它运行了相关的过滤器。

So, how can I highlight a given item on a context menu display event? 因此,如何在上下文菜单显示事件中突出显示给定的项目?

Thanks 谢谢

Trigger a style setter bound to the context menu's IsOpen property. 触发绑定到上下文菜单的IsOpen属性的样式设置器。 I got this to work using a DataTrigger in a Style on the item like this: 我使用像这样的样式中的DataTrigger来工作:

<Style TargetType="{x:Type TreeViewItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Obviously you can choose something more appropriate to do than turning the text red (like maybe making the background look selected), but that's the general idea. 显然,您可以选择比将文本变成红色更适合做的事情(例如,使背景看起来处于选中状态),但这是一般的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM