我可以在 wpf 分层数据模板上参数化数据源吗?
我有一个带有两个相似子树的分层数据结构:
Iteration:
string Name
string Image
ObservableCollection<Iteration> SubIterations
ObservableCollection<Iteration> BacklogIterations
我想使用两个树视图来呈现两个略有不同的树视图。
- Tree1:显示子迭代
- Tree2:显示 BacklogIterations
元素应显示相同,但需要使用不同的属性来选取子项。
即我想参数化 HierarchicalDataTemplate 中的 ItemsSource:
<HierarchicalDataTemplate x:Key="IterationItem"
ItemsSource="{Binding SubIterations}"
>
<StackPanel Orientation="Horizontal">
<Image Width="32" Height="32"
Margin="3,0" Source="{Binding Picture}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<TreeView ItemsSource="{Binding RootSprintIteration}">
</TreeView>
<TreeView ItemsSource="{Binding RootBacklogIteration}">
</TreeView>
如何获取第二个树中的 HierarchicalDataTemplate 以使用不同的 ItemsSource? 我不想复制整个 HierarchicalDataTemplate 定义只是为了更改 ItemsSource。
I have a hierarchical data structure with two similar sub-trees:
Iteration:
string Name
string Image
ObservableCollection<Iteration> SubIterations
ObservableCollection<Iteration> BacklogIterations
I want to present two slightly different views of the tree, using two tree views.
- Tree1: Display the SubIterations
- Tree2: Display the BacklogIterations
The elements should be displayed the same, but the child items need to be picked up using a different property.
i.e. I want to parameterize the ItemsSource in the HierarchicalDataTemplate:
<HierarchicalDataTemplate x:Key="IterationItem"
ItemsSource="{Binding SubIterations}"
>
<StackPanel Orientation="Horizontal">
<Image Width="32" Height="32"
Margin="3,0" Source="{Binding Picture}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<TreeView ItemsSource="{Binding RootSprintIteration}">
</TreeView>
<TreeView ItemsSource="{Binding RootBacklogIteration}">
</TreeView>
How can I get the HierarchicalDataTemplate in the second tree to use a different ItemsSource?
I do not want to duplicate the whole HierarchicalDataTemplate definition just to change the ItemsSource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将数据的 Viauslization 放置在 DataTemplate 中,并创建两个 HierarchicalDataTemplate,其中将 ItemTemplate 设置为创建的 DataTemplate。没有理由不应该创建两个 HierarchicalDataTemplate。它们完全为您提供了您想要的参数化,并允许您对两者使用相同的 ItemTemplate
Place the Viauslization of the data in a DataTemplate and create two HierarchicalDataTemplates where you set the ItemTemplate to the created DataTemplate. There exist no reason why you shouoldnt create two HierarchicalDataTemplates. They offer you exactly the parametrization you wish and allow you to use the same ItemTemplate for both