1

ツリー構造のデータグリッドに階層データを表示する方法を知りたいです。シナリオを明確にします

私は次のクラスを持っています。

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を使用してこれを行うにはどうすればよいですか。
それは非常に役に立ちます、これは私を何日も夢中にさせています。

4

1 に答える 1

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>

私はそれをテストしていません。そうでない場合は、私に知らせてください。もっと説明してみる

于 2012-11-19T11:53:14.990 に答える