【问题标题】:TreeTable control in WPF and c#WPF和c#中的TreeTable控件
【发布时间】:2012-11-19 11:35:41
【问题描述】:

我想知道如何在具有树结构的数据网格中显示分层数据。我会清楚地说明这个场景

我有以下课程..

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 完成此操作。
这将非常有帮助,这让我疯狂了好几天。

【问题讨论】:

  • 你可以看看这个已经问过的question

标签: c# wpf datagrid treeview


【解决方案1】:

你需要在 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>

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

【讨论】:

  • 嗨,我是.net 新手,我来自java 背景,对.net 和wpf 知之甚少。如果你不介意你能解释一下你做了什么以及它是如何工作的吗?或者指出一些我可以清楚地了解这一点的材料。
  • 在玩过代码之后,我认为最好使用 TreeView,所以请参阅@Viktor La Croix 对您的问题的评论(或此blogs.microsoft.co.il/blogs/davids/archive/2010/04/17/…);如果你需要 DataGrid,你可以看这篇 msdn 文章:msdn.microsoft.com/ru-ru/library/dd833072(v=vs.95).aspx
  • 只是一个评论:如果你来自 Java 背景,并且必须在 WPF 中工作,那么忘记你所学的一切。 WPF 需要相对于任何其他技术进行巨大的思维转变。您必须将 UI 与数据(和功能)分开。看看 MVVM 模式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多