当前位置: 首页 > 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;
}

http://www.lryc.cn/news/419952.html

相关文章:

  • 给python初学者的一些建议
  • 【Python学习手册(第四版)】学习笔记17-作用域
  • 大语言模型(LLM)文本预处理实战
  • 宠物健康新守护:智能听诊器的家庭应用
  • 六、go函数
  • 高原型垂直起降高速无人机技术详解
  • Selenium + Python 自动化测试10(unittest概念)
  • 大数据-67 Kafka 高级特性 分区 分配策略 Ranger、RoundRobin、Sticky、自定义分区器
  • 深度学习与图像修复:ADetailer插件在Stable Diffusion中的应用
  • 【Pytorch】topk函数
  • 使用mybatis注解和xml映射执行javaWeb中增删改查等操作
  • SpringBoot3 响应式编程
  • 【C++ 面试 - 基础题】每日 3 题(二)
  • Modelica建模,Modelica语言的学习,技术调研工作
  • Oracle数据字典之——v$lock 和v$locked_object
  • solidity 以太坊(Ether) 单位(很基础)
  • 关于elementUI 分页 table 使用 toggleRowSelection
  • K8s部署RocketMQ
  • Linux服务管理-Nginx配置
  • C语言典型例题31
  • FFMPEG 工具方法
  • Qt QML 使用QPainterPath绘制弧形曲线和弧形文本
  • VMware虚拟机和Docker的备份与恢复
  • 新加坡服务器延迟大吗?如何进行优化
  • uniapp——列表图片加载太多且空间占用太大的处理方法(降低清晰度)
  • spring+SSM+Mybatis面试题(上)(30道)
  • odoo17 翻译一个小bug
  • sqli-labs-php7-master第5-10关
  • 全方位教程:接入视频美颜SDK与直播美颜插件的完整步骤
  • Apache Curator 分布式锁的介绍,以及案例