由于项目需要,需要能够定位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. debian9安装mysql mariadb

    debian9下mysql 替换成mariadb-server-10.1 不过两者类似 具体可见 <MySQL和mariadb区别> http://ask.chinaunix.net/qu ...

  2. windows mysql 主从热备

    环境说明:   Master:192.168.1.200 Slave:192.168.1.210     MySQL 的 Master 配置:   配置my.ini:   [mysqld]   # T ...

  3. 内存溢出和内存泄漏的区别(ZZ)

      内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory:比如申请了一个integer,但给它存了long才能存下的数,那就是内存溢出 ...

  4. PDO beginTransaction (),exec(),commit ()

    $dsn = 'sqlsrv:server=.\SQLExpress;Database=thinkphp'; $user = 'admin'; $password = 'pass1234'; try ...

  5. NOIP 2017 d2t2 70points

    革命之路漫漫 第一次尝试 40points spfa #include <bits/stdc++.h> #define read read() using namespace std; i ...

  6. vue.js实战(文摘)

    ---------------第1篇 基础篇 第1章 初始vue.js 第2章 数据绑定和第一个vue应用 第3章 计算属性 第4章 v-bind及class与style绑定 第5章 内置命令 第6章 ...

  7. (17)Questioning the universe

    https://www.ted.com/talks/stephen_hawking_asks_big_questions_about_the_universe/transcript00:13There ...

  8. (3)The critical role librarians play in the opioid crisis

    https://www.ted.com/talks/chera_kowalski_the_critical_role_librarians_play_in_the_opioid_crisis 00:1 ...

  9. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  10. Arbiter

    from  2015-EDCAV-Problems encountered in various arbitration techniques used in NOC router-A survey ...