当前位置: 首页 > news >正文

WPF Treeview开启虚拟化后如何找到TreeViewItem

 用VirtualizingStackPanel的BringIndexIntoViewPublic方法就好,没必要像微软给的例子那样还要继承一个VirtualizingStackPanel

/// <summary>
/// Recursively search for an item in this subtree.
/// </summary>
/// <param name="container">
/// The parent ItemsControl. This can be a TreeView or a TreeViewItem.
/// </param>
/// <param name="item">
/// The item to search for.
/// </param>
/// <returns>
/// The TreeViewItem that contains the specified item.
/// </returns>
private TreeViewItem GetTreeViewItem(ItemsControl container, object item)
{if (container != null){if (container.DataContext == item){return container as TreeViewItem;}// Expand the current containerif (container is TreeViewItem && !((TreeViewItem)container).IsExpanded){container.SetValue(TreeViewItem.IsExpandedProperty, true);}// Try to generate the ItemsPresenter and the ItemsPanel.// by calling ApplyTemplate.  Note that in the// virtualizing case even if the item is marked// expanded we still need to do this step in order to// regenerate the visuals because they may have been virtualized away.container.ApplyTemplate();ItemsPresenter itemsPresenter =(ItemsPresenter)container.Template.FindName("ItemsHost", container);if (itemsPresenter != null){itemsPresenter.ApplyTemplate();}else{// The Tree template has not named the ItemsPresenter,// so walk the descendents and find the child.itemsPresenter = FindVisualChild<ItemsPresenter>(container);if (itemsPresenter == null){container.UpdateLayout();itemsPresenter = FindVisualChild<ItemsPresenter>(container);}}Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);// Ensure that the generator for this panel has been created.UIElementCollection children = itemsHostPanel.Children;VirtualizingStackPanel virtualizingPanel =itemsHostPanel as VirtualizingStackPanel;for (int i = 0, count = container.Items.Count; i < count; i++){TreeViewItem subContainer;if (virtualizingPanel != null){// Bring the item into view so// that the container will be generated.virtualizingPanel.BringIndexIntoViewPublic(i);subContainer =(TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);}else{subContainer =(TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);// Bring the item into view to maintain the// same behavior as with a virtualizing panel.subContainer.BringIntoView();}if (subContainer != null){// Search the next level for the object.TreeViewItem resultContainer = GetTreeViewItem(subContainer, item);if (resultContainer != null){return resultContainer;}else{// The object is not under this TreeViewItem// so collapse it.subContainer.IsExpanded = false;}}}}return null;
}/// <summary>
/// Search for an element of a certain type in the visual tree.
/// </summary>
/// <typeparam name="T">The type of element to find.</typeparam>
/// <param name="visual">The parent element.</param>
/// <returns></returns>
private T FindVisualChild<T>(Visual visual) where T : Visual
{for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++){Visual child = (Visual)VisualTreeHelper.GetChild(visual, i);if (child != null){T correctlyTyped = child as T;if (correctlyTyped != null){return correctlyTyped;}T descendent = FindVisualChild<T>(child);if (descendent != null){return descendent;}}}return null;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Docker容器里centos、unbuntu无法使用 systemctl 命令
  • 更换到阿里云的 CentOS 7 镜像源
  • AutoSar AP软件规范中EM介绍及功能概要
  • 新技术环境下保密工作面临的挑战和应对策略
  • 【Python】Python中一些有趣的用法
  • canal监听mysql增量数据发布到rabbitmq
  • JavaWeb—XML_Tomcat10_HTTP
  • PLSQL导入导出ORACLE数据提示失败问题修改PLSQL配置
  • 目标检测研究方向——开放域目标检测
  • 【docker】虚拟化与docker基础
  • Android SurfaceFlinger——Vsync信号发送(五十二)
  • Ant-Design-Vue快速上手 指南+排坑(上)
  • IDEA连接数据库
  • 谈对象系列:C++类和对象
  • WPF界面开发-C#单例模式实现 页面数据传输
  • JS 中的深拷贝与浅拷贝
  • Angular 响应式表单 基础例子
  • const let
  • JS数组方法汇总
  • leetcode386. Lexicographical Numbers
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • REST架构的思考
  • Vue 重置组件到初始状态
  • Webpack入门之遇到的那些坑,系列示例Demo
  • Xmanager 远程桌面 CentOS 7
  • 动态规划入门(以爬楼梯为例)
  • 对超线程几个不同角度的解释
  • 给第三方使用接口的 URL 签名实现
  • 力扣(LeetCode)21
  • 排序(1):冒泡排序
  • 前端性能优化——回流与重绘
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 说说动画卡顿的解决方案
  • 携程小程序初体验
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • ​必胜客礼品卡回收多少钱,回收平台哪家好
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (LeetCode C++)盛最多水的容器
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (六)c52学习之旅-独立按键
  • (十八)SpringBoot之发送QQ邮件
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (图)IntelliTrace Tools 跟踪云端程序
  • (新)网络工程师考点串讲与真题详解
  • (原创)可支持最大高度的NestedScrollView
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .NET MVC 验证码
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .NET开源的一个小而快并且功能强大的 Windows 动态桌面软件 - DreamScene2
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析