WPF和c#中的TreeTable控件

时间:2012-11-19 11:35:41

标签: c# wpf datagrid treeview

我想知道如何在具有树结构的数据网格中显示分层数据。我将通过方案

清楚地表明自己

我有以下课程..

public class Package
{
    public String name { get; set; }
    public Measure measure { get; set; }
    public List<Class> classList { get; set; }
}

public class Class
{
    public String name { get; set; }
    public Measure measure { get; set; }
    public List<Method> methodsList { get; set; }
}

public class Method
{
    public String name { get; set; }
    public Measure measure { get; set; }
}

public class Measure
{
    public String tolc { get; set; }
    public String lloc { get; set; }
    public String ploc { get; set; }
    public String lComments { get; set; }
    public String blankLines { get; set; }
}

现在我想在一个像这样的树结构的数据网格中显示它们。

Item Name    TLOC    LLOC   PLOC   LCOMMENTS    BLANKLINES
package1     100     80     70     45           30
-class1      30      20     19     2            12
--method1    30      20     19     2            12
-class2      70      60     51     43           18
--method1    50      20     11     23           8
--method2    20      40     40     20           10
package2     50      20     10     5            5
-class       50      20     10     5            5

希望我说清楚。如何在C#中使用WPF完成此任务 这将非常有帮助,这让我疯狂了几天。

1 个答案:

答案 0 :(得分:0)

您需要在XAML中使用CollectionViewSource,如下所示:

<CollectionViewSource x:Key="Packages" Source="{Binding AllCode}">
            <CollectionViewSource.SortDescriptions>
                <!-- Requires 'xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"' declaration. -->
                <scm:SortDescription PropertyName="Name"/>
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="PackageName"/>
                <PropertyGroupDescription PropertyName="ClassName"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

然后你需要使用组样式附加数据网格:

<DataGrid x:Name="dataGrid1"
                  Background="{x:Null}"
                  ItemsSource="{Binding Source={StaticResource Packages}}"
        <DataGrid.Columns>
                <DataGridTextColumn DisplayMemberPath="PackageName"/>
                <DataGridTextColumn DisplayMemberPath="ClassName"/>
            </DataGrid.Columns>
            <DataGrid.GroupStyle>
                <!-- Style for groups at top level. -->
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">                            
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True" Background="White">
                                            <Expander.Header>
                                                <DockPanel>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Name}" Margin="5,0"/>
                                                    <TextBlock FontWeight="Bold" Text="{Binding ItemCount}" Margin="5,0"/>
                                                </DockPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>

我不测试它,所以我希望你有所了解。如果没有,请告诉我。我尝试解释更多