开发者

WPF: styling a HierarchicalDataTemplate

开发者 https://www.devze.com 2023-04-12 07:35 出处:网络
Here is a simple DataTemplate <Grid.Resources> <HierarchicalDataTemplate ItemsSource=\"{Binding Items}\" DataType=\"{x:Type entities:Folder}\" ItemContainerStyle=\"{StaticResource FileCompon

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 styl开发者_JAVA百科e 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消