WPF:HierarchicalDataTemplate ItemsPanel

时间:2010-04-08 13:30:35

标签: wpf styles datatemplate itemscontrol hierarchicaldatatemplate

我有一个``TreeView which uses a custom ItemsPanel to show the first level of items in a StackPanel , but I need to show subitems in a StackPanel too. The problem is, the second level of items are shown in a WrapPanel , and as HierarchicalDataTemplate doesn't have an ItemsPanel`属性我是不知道该怎么做。这是我的xaml:

<TreeView x:Name="treGlobalCards">
    <TreeView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="{Binding Orientation,RelativeSource={x:Static RelativeSource.TemplatedParent}}"
                        MaxWidth="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}"/>
        </ItemsPanelTemplate>
    </TreeView.ItemsPanel>
    <TreeView.ItemTemplate>
    <HierarchicalDataTemplate x:Key="CardTypeTemplate" ItemsSource="{Binding Cards}">
        <TextBlock Text="{Binding Path=CardType}"/>
    </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

1 个答案:

答案 0 :(得分:0)

创建一个使用StackPanel的新DataTemplate,并将HierachicalDataTemplate的“ItemTemplate”设置为新的DataTemplate。


<DataTemplate x:Key="someTemp">
    <StackPanel />
</DataTemplate>

<HierarchicalDataTemplate x:Key="hierTemp" ItemSource="{Binding}" ItemTemplate="{StaticResource someTemp}" />