在TreeView中显示相同类型的集合

时间:2016-07-27 13:30:32

标签: wpf xaml data-binding treeview hierarchicaldatatemplate

我想显示以下结构

Duplication
    DuplicateLine1
           filename_startline_endline
           filename_startline_endline
           filename_startline_endline
         MORE OCCURENCES in OTHER COMPONENTS
           filename_startline_endline
           filename_startline_endline
           filename_startline_endline
    DuplicateLine2
           filename_startline_endline
           filename_startline_endline
           filename_startline_endline
         MORE OCCURENCES in OTHER COMPONENTS
           filename_startline_endline
           filename_startline_endline
           filename_startline_endline

这是我的DataStructure

public class CloneBlock
{
    public string DuplicateLine;
    public List<Occurrence> Occurrences { get; set; }
    public List<Occurrence> OtherOccurrences { get; set; }
}

正如您所看到的,我在一个类中有两个相同类型的集合。 我试图使用HierarchialDataTemplate显示它,但没有成功。

问题是只有一个集合可以显示为类型相同。

XAML:

        <Grid.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:CloneBlock}"
                                      ItemsSource="{Binding Path=Occurrences}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Background='AliceBlue' Text='{Binding CloneDisplayline}' />
                    <TextBlock Background='Aqua' FontSize="12" Width="20" FontWeight="SemiBold" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text='{Binding OccurrenceCount}' />
                </StackPanel>
            </HierarchicalDataTemplate>
            <DataTemplate
                    DataType="{x:Type local:Occurrence}">
                <TextBlock Text='{Binding FileId}' />
            </DataTemplate>
        </Grid.Resources>
        <TextBlock Grid.Row="0" Text="Duplication" Height="20" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="5,0,0,0" />
        <TreeView Grid.Row="1" ItemsSource="{Binding CloneBlocks}">

任何想法?怎么做?

提前致谢。

0 个答案:

没有答案