WPF TreeView BringIntoViewBehavior
由于项目需要,需要能够定位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的更多相关文章
- WPF TreeView SelectedItemChanged called twice
How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some ...
- WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...
- WPF TreeView Indent 减少节点的缩进
www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...
- WPF TreeView的使用
WPF提供了treeView控件,利用该控件开发者可以将数据分层显示在树结构中.当然其中需要用到Binding的机制,有用的类包括:ObjectDataProvider.DataTemplate.Hi ...
- WPF TreeView 虚拟化-设置滚动到选中项
前言 列表滚动到具体的数据项? ListBox提供了简易快捷的滚动定位函数ScrollIntoView. TreeView树状结构列表,则没有此类方法,无法与ListBox一样,直接设置滚动到具体的数 ...
- 【原创】WPF TreeView带连接线样式的优化(WinFrom风格)
一.前言 之前查找WPF相关资料的时候,发现国外网站有一个TreeView控件的样式,是WinFrom风格的,样式如下,文章链接:https://www.codeproject.com/tips/67 ...
- wpf TreeView
<Window x:Class="WpfTutorialSamples.TreeView_control.TreeViewDataBindingSample" ...
- WPF TreeView绑定字典集合
<TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...
- WPF—TreeView无限极绑定集合形成树结构
1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...
随机推荐
- db2 解锁表
db2 set integrity for ACT_RU_VARIABLE immediate checked
- TabControl中显示和隐藏TabPage页
在使用TabControl控件时,希望隐藏其中某个选项卡(即TabPage).TabPage类明明提供了一个Hide方法,用在代码中却没有任何效果,甚是奇怪.无奈之余,只好考虑另辟途径 方法一: 设置 ...
- 棋盘问题(NOIP1997)
题目链接:棋盘问题 这道题水不水呢?还是很水的,为什么?因为数据太小了.直接算就行了. #include<bits/stdc++.h> using namespace std; int m ...
- 哈希与字典树与KMP
hash讲解 主要记录hash的公式: ; i<=len; i++) { Hash[i]=(Hash[i-]*)%mod)%mod; } 求hash的公式是这个,怎么求一小段的hash值呢? ; ...
- O365 Manager Plus详解
- Sprign中常用注解
1.@Component 创建类对象,相当于配置<bean/> 2.@Service 与 @Component功能相同 2.1写在ServiceImpl类上 (建议在ServiceImpl ...
- java 模拟登录新浪微博(通过cookie)
这几天一直在研究新浪微博的爬虫,发现爬取微博的数据首先要登录.本来打算是通过账号和密码模拟浏览器登录.但是现在微博的登录机制比较复杂.通过账号密码还没有登录成功QAQ.所以就先记录下,通过cookie ...
- 2018.11.24 loj#111. 后缀排序(后缀数组)
传送门 后缀排序模板题. 终于会后缀数组了(然而只会倍增并不会DC3DC3DC3). 在这里列举几个数组的意思: sai:sa_i:sai:当前排名第iii的后缀的起始下标. rkirk_irki ...
- mysql 外键理解
假定一个班级的学生个人信息表: 什么是外键 在设计的时候,就给表1加入一个外键,这个外键就是表2中的学号字段,那么这样表1就是主表,表2就是子表.(注意: 外键不一定须要作为从表的主键.外键也不一定是 ...
- android 自动更新
http://blog.csdn.net/zml_2015/article/details/50756703