问题描述
我对 WPF 还很陌生,我正在尝试编写某种资源管理器,因此我需要创建一个自定义的 Microsoft.Xaml.Behaviors.Behavior
。
为了自动展开并选择合适的 TreeViewItem
,我需要遍历相应项目的所有 DataContext
。
遗憾的是,使用 ItemsControl.ItemContainerGenerator.ContainerFrom[...]
的常见建议有一个很大的缺点:它只为已经绘制的 TreeViewItem
提供结果。
即使由于下面发布的 XAML 代码,框架基本上知道如何为每种类型的 ItemsSource
获取 DataContext
,但我发现自己无法以编程方式提取 {{1 ItemsSource
的 }}。
DataContext
https://stackoverflow.com/a/35565892
因此,在此示例中,框架实际上应该知道如何根据我的 <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>
-code 从 classB
- 和 classC
-types 的实例中获取子项。
我正在寻找的是根据 xaml
中声明的 HierarchicalDataTemplate
递归获取子项的某种方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)