由于项目需要,需要能够定位TreeView中的点,TreeView的节点数过多的情况下,即使找到了对应的节点并选中展示了,由于不在可视区域内,给用户的感觉还是不好,因此设计如下的Behavior,来实现选中的TreeViewItem显示在可见区域:

 using System;
using System.Windows;
using System.Windows.Controls; namespace Johar.Core
{
public static class BringIntoViewBehavior
{
public static bool GetIsBringIntoViewWhenSelected(DependencyObject obj)
{
return (bool)obj.GetValue(IsBringIntoViewWhenSelectedProperty);
} public static void SetIsBringIntoViewWhenSelected(DependencyObject obj, bool value)
{
obj.SetValue(IsBringIntoViewWhenSelectedProperty, value);
} // Using a DependencyProperty as the backing store for IsBringIntoViewWhenSelected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsBringIntoViewWhenSelectedProperty =
DependencyProperty.RegisterAttached("IsBringIntoViewWhenSelected",
typeof(bool), typeof(BringIntoViewBehavior),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsBringIntoViewWhenSelected))); private static void OnIsBringIntoViewWhenSelected(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TreeViewItem item = d as TreeViewItem;
if (item == null)
{
return;
} if (e.NewValue is bool == false)
{
return;
} if ((bool)e.NewValue)
{
item.Selected -= item_Selected;
item.Selected += item_Selected;
}
else
{
item.Selected -= item_Selected;
}
} private static void item_Selected(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item != null)
{
item.BringIntoView();
}
}
}
}

然后在TreeView中设置一下这个依赖属性为True即可上述要求。

WPF TreeView BringIntoViewBehavior的更多相关文章

  1. WPF TreeView SelectedItemChanged called twice

    How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some ...

  2. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

  3. WPF TreeView Indent 减少节点的缩进

    www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...

  4. WPF TreeView的使用

    WPF提供了treeView控件,利用该控件开发者可以将数据分层显示在树结构中.当然其中需要用到Binding的机制,有用的类包括:ObjectDataProvider.DataTemplate.Hi ...

  5. WPF TreeView 虚拟化-设置滚动到选中项

    前言 列表滚动到具体的数据项? ListBox提供了简易快捷的滚动定位函数ScrollIntoView. TreeView树状结构列表,则没有此类方法,无法与ListBox一样,直接设置滚动到具体的数 ...

  6. 【原创】WPF TreeView带连接线样式的优化(WinFrom风格)

    一.前言 之前查找WPF相关资料的时候,发现国外网站有一个TreeView控件的样式,是WinFrom风格的,样式如下,文章链接:https://www.codeproject.com/tips/67 ...

  7. wpf TreeView

    <Window x:Class="WpfTutorialSamples.TreeView_control.TreeViewDataBindingSample"        ...

  8. WPF TreeView绑定字典集合

    <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...

  9. WPF—TreeView无限极绑定集合形成树结构

    1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...

随机推荐

  1. mysql 设置用户并授权

    一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...

  2. AJAX-DOM事件

    1.DOM事件 1.select的onchange事件 当选项框中的内容发生改变时需要出发的事件.2.Ajax 1.名词解释 1.同步 在一个任务进行中,不能开启其它的任务. 同步访问:浏览器在向服务 ...

  3. 【技术课堂】如何管理MongoDB数据库?

  4. Ajax的爬取心得

    一.查找到js的网址 在我们做爬虫的时候,如何判断一个数据是Ajax(asynchronous JavaScript And Xml,异步的JavaScript和Xml), 首先是数据的加载,在请求网 ...

  5. vue 开发系列(五) 调用原生API

    概要 我们在开发手机端程序的时候了,我们经常需要使用到拍照,二维码的功能.数字天堂公司提供了大量的原生API支持. http://www.html5plus.org/doc/ 实现 1.在hbuild ...

  6. Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)

    https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...

  7. JavaScript 模拟键盘事件和鼠标事件(比如模拟按下回车等)

    http://blog.csdn.net/lovelyelfpop/article/details/52471878# 封装好的function大概就是这样: function fireKeyEven ...

  8. c#在panel或groupbox中添加窗体,实现点击不同按钮或combox时panel中窗体切换,在xtratabcontrol中添加窗体

    参考panel添加窗体: http://blog.csdn.net/illegalname/article/details/65444249 http://blog.csdn.net/Eastmoun ...

  9. HDMI之HPD

    HDMI(19Pin)/DVI(16 pin)的功能是热插拔检测(Hot Plug Detect,HPD),这个信号将作为主机系统是否对HDMI/DVI是否发送TMDS信号的依据.HPD是从显示器输出 ...

  10. Codeforces Round#412 Div.2

    A. Is it rated? 题面 Is it rated? Here it is. The Ultimate Question of Competitive Programming, Codefo ...