WPF TreeView突出显示行悬停 [英] WPF TreeView Highlight Row On Hover

查看:385
本文介绍了WPF TreeView突出显示行悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,悬停在TreeView中的标题将突出显示标题。我想将鼠标悬停在TreeView的任何部分,以突出显示整个行(很像Windows资源管理器)。有人可以提供一个如何做到这一点的例子吗?



ControlTemplate > 对于 TreeViewItem 实际上就是控件的整个宽度。



有关这个问题的讨论,沿有一个解决方案,在这里:



http://leecampbell.blogspot.com/2009/01/horizo​​ntal-stretch-on-treeviewitems.html



还有另一个冗长的答案在这里,没有讨论:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b04f73e2-0b10-4d97-a6da-64df2e30c21d/



所以我的意思是这些例子成为导游。如果你采用与上面第二个链接(来自MSDN论坛)完全相同的代码,只需添加:

 <触发属性=IsMouseOver值=真> 
< Setter Property =BackgroundTargetName =Bd
Value ={DynamicResource {x:Static SystemColors.HighlightBrushKey}}/>
< /触发>

添加到 ControlTemplate 中,您会看到你会在鼠标悬停上获得高亮颜色。显然你需要调整颜色和内容,但这就是你需要做的 - 修改 TreeViewItem ControlTemplate c $ c>,以便占用整个宽度并为 IsMouseOver 添加 Trigger


Currently, hovering over a header in the TreeView will highlight the header. I would like hovering over any part of the row in the TreeView to highlight the entire row (much like windows explorer does). Could someone provide an example of how to do that?

解决方案

You need to change the ControlTemplate for the TreeViewItem to actually be the entire width of the control.

There is a discussion about this issue, along with a solution, here:

http://leecampbell.blogspot.com/2009/01/horizontal-stretch-on-treeviewitems.html

There's another lengthy answer here, without the discussion:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b04f73e2-0b10-4d97-a6da-64df2e30c21d/

So I meant those examples to be guides. If you take exactly the same code as is in the second link above (from MSDN forums) and just add this:

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" TargetName="Bd"
            Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>

to the ControlTemplate you'll see that you get the highlight color on mouseover. Obviously you'll need to tweak the color and whatnot, but that's what you'll need to do - modify the ControlTemplate of the TreeViewItem so that it takes up the entire width and add a Trigger for IsMouseOver.

这篇关于WPF TreeView突出显示行悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆