c# - 如何递归地遍历 TreeView-items
问题描述
我对 WPF 很陌生,我正在尝试编写某种资源管理器,因此我需要创建一个自定义的Microsoft.Xaml.Behaviors.Behavior
.
为了自动展开并选择合适TreeViewItem
的s,我需要遍历所有DataContext
s的对应项。
可悲的是,使用的常见建议ItemsControl.ItemContainerGenerator.ContainerFrom[...]
有一个很大的缺点:它只为TreeViewItem
已经绘制的 s 提供结果。
即使框架基本上知道如何通过下面发布的 XAML 代码获取ItemsSource
每种类型的.DataContext
ItemsSource
DataContext
<TreeView ItemsSource="{Binding Items}"> <TreeView.Resources> <DataTemplate DataType="{x:Type local:classA}" > <TextBlock Text="{Binding name}" Foreground="Blue" /> </DataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:classB}" ItemsSource="{Binding subItems}" > <TextBlock Text="{Binding name}" Foreground="Green" /> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:classC}" ItemsSource="{Binding subItems}" > <TextBlock Text="{Binding name}" Foreground="Red" /> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView>
所以,在这个例子中,框架实际上应该知道如何根据我的代码从classB
- 和 -类型的实例中获取子项。我正在寻找的是基于在.classC
xaml
HierarchicalDataTemplate
xaml
解决方案
推荐阅读
- c# - 大括号和引号的自动完成在 Visual Studio C# 中不起作用
- system-verilog - 系统verilog中[$]运算符在声明类对象时的功能
- javascript - 如果值存在,请执行此操作。未定义与空?
- python - 在 Python 3 中获取 PostgreSQL REFCURSOR
- asp.net-mvc - 回发到 HTTP GET 后 ASP.NET MVC 恢复表单元素
- javascript - 如何将对象存储在 Json 文件中
- python - 为什么我会在 python 中遇到这个网络抓取问题?
- python - Python Tkinter 绑定
- c++ - 使在线程上运行的 boost::steady_timer 到期
- python - Pytables 复制 2.5 giga 行