首页 > 解决方案 > 使用父对象作为扩展标题的 WPF 数据网格分组

问题描述

我正在使用 WPF Datagrid 对 Parent 的可观察集合进行分组。我一直在关注这里的示例和其他显示父子关系的示例。到目前为止,我有以下内容:

在此处输入图像描述

我想得到这样的东西:

在此处输入图像描述

组的标题也只是一行,但仍然能够折叠/展开子行。我试图在没有运气的情况下制作一个子数据网格。所以我的集合的底层类型看起来像这样:

public class Task : INotifyPropertyChanged, IEditableObject
{
    // memebers
    ...

    public string ProjectName
    {
        get { return this.m_ProjectName; }
        set
        {
            if (value != this.m_ProjectName)
            {
                this.m_ProjectName = value;
                NotifyPropertyChanged("ProjectName");
            }
        }
    }

    public string TaskName
    {
        get { return this.m_TaskName; }
        set
        {
            if (value != this.m_TaskName)
            {
                this.m_TaskName = value;
                NotifyPropertyChanged("TaskName");
            }
        }
    }

    public DateTime DueDate
    {
        get { return this.m_DueDate; }
        set
        {
            if (value != this.m_DueDate)
            {
                this.m_DueDate = value;
                NotifyPropertyChanged("DueDate");
            }
        }
    }

    public bool Complete
    {
        get { return this.m_Complete; }
        set
        {
            if (value != this.m_Complete)
            {
                this.m_Complete = value;
                NotifyPropertyChanged("Complete");
            }
        }
    }

    public Task Parent
    {
        get { return m_Parent; }
        set
        {
            m_Parent = value;
        }
    }

所以我的对象类型可以是子对象或父对象,其中子对象引用其父对象。所以我按父分组,我只是还没有弄清楚如何使可扩展组标题行具有相同类型。任何帮助表示赞赏。

这是xaml:

<DataGrid x:Name="dataGrid1" 
              ItemsSource="{Binding Source={StaticResource cvsTasks}}"
              CanUserAddRows="False"
              ColumnWidth="*"
              RowHeaderWidth="0">
        <DataGrid.GroupStyle>
            <!-- Style for groups at top level. -->
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,2"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" Background="#FF112255" BorderBrush="#FF002255" Foreground="#FFEEEEEE" BorderThickness="1,1,1,5">
                                        <Expander.Header>
                                            <DockPanel HorizontalAlignment="Stretch" >
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Converter={StaticResource completeConverter}}" Margin="5,0,0,0" Width="200" HorizontalAlignment="Stretch"/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" Width="Auto"/>

                                            </DockPanel>
                                            <!--<DataGrid x:Name="dataGrid2" 
                                                          ItemsSource="{Binding Source={StaticResource vsParentTasks}}"
                                                          CanUserAddRows="False"
                                                          ColumnWidth="*"
                                                          RowHeaderWidth="0"
                                                          HeadersVisibility="None" >
                                                    <DataGrid.RowStyle>
                                                        <Style TargetType="DataGridRow">
                                                            <Setter Property="Foreground" Value="#FFEEEEEE" />
                                                            <Setter Property="Background" Value="#FF112255" />
                                                            <Setter Property="HorizontalAlignment" Value="Stretch" />
                                                    </Style>
                                                    </DataGrid.RowStyle>
                                                </DataGrid>-->
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="Background" Value="White" />
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

标签: c#wpfxaml

解决方案


推荐阅读