2011-10-09 63 views
0

下面是一個簡單的DataTemplateWPF:造型一HierarchicalDataTemplate

<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的子節點會得到樣式(無論項目在Folder下展開,而Folder本身仍然沒有樣式)。

我希望能夠控制項目的風格模板所應用,而不是孩子

回答

1

既然你應用該樣式的ItemContainer和顧名思義它會被應用到它包含在這種情況下,將文件夾的子節點的項目。 因此,您需要應用此style on your listbox或此Hierarchichal數據模板得到應用的任何控件。

+0

可能做錯了,但是當我覆蓋列表框模板,我失去的分層元素。 –

+0

我們可以看到您的代碼爲您的列表框? –

+0

我實際上使用它與TreeView ..(更新)。我還應該模板一個列表框? –