WPF:设计 HierarchicalDataTemplate 的样式
这是一个简单的 DataTemplate,
<Grid.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Items}" DataType="{x:Type entities:Folder}" ItemContainerStyle="{StaticResource FileComponentItem}">
<Grid ShowGridLines="False" HorizontalAlignment="Center">
<TextBlock Grid.Column="0" Text="{Binding Type}" />
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ...... />
</Grid.Resources>
它应用于 TreeView
<TreeView Grid.Row="1" BorderThickness="0" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource FlattenedTreeViewItem}"></TreeView>
请注意,样式 FileComponentItem 应用于类型实体的模板:Folder
它有效,接受样式仅应用于此节点(文件夹节点)的子节点。 这意味着,只有文件夹的子节点才会获得样式(无论在文件夹下展开什么项目,而文件夹本身仍然没有样式)。
我希望能够控制应用模板的项目的样式,而不是子项的样式
Here is a simple DataTemplate
<Grid.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Items}" DataType="{x:Type entities:Folder}" ItemContainerStyle="{StaticResource FileComponentItem}">
<Grid ShowGridLines="False" HorizontalAlignment="Center">
<TextBlock Grid.Column="0" Text="{Binding Type}" />
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ...... />
</Grid.Resources>
this is applied to TreeView
<TreeView Grid.Row="1" BorderThickness="0" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource FlattenedTreeViewItem}"></TreeView>
Notice that a style FileComponentItem is applied to a template for type entities:Folder
It works, accept the style is only applied to the children of this node (Folder node).
meaning, only the child nodes of Folder, will get the style (whatever items are expanded under Folder, while Folder itself remains not styled).
I would like to be able to control the style of items to which the template is applied, not the children
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您将样式应用于
ItemContainer
,并且顾名思义,它将应用于它包含的项目,在本例中将是文件夹的子节点。因此,您需要在列表框或应用此层次结构数据模板的任何控件上应用此样式。
Since you applied the style to the
ItemContainer
and as the name suggests it will be applied to the items it contains which in this case will be the child nodes of folder.So, you need to apply this
style on your listbox
or any control where this Hierarchichal data template gets applied.查看 http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx 的
解释和相应的示例应该解决你的问题。
Look at http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx
The explanation and corresponding sample should take care of your question.