【问题标题】:WPF Datagrid Grouping by two columns - LayoutWPF Datagrid 按两列分组 - 布局
【发布时间】:2016-01-04 05:43:40
【问题描述】:

我想按两列对我的数据网格进行分组。姓名和姓氏。 它基本上可以工作,但它弄乱了我的整个布局。

我的代码:

Xaml:

<DataGrid x:Name="dgErfasst" RowEditEnding="dgMitarbeiter_RowEditEnding" AutoGenerateColumns="False" Margin="0,23,8,53" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" HeadersVisibility="Column" Background="White" SelectionMode="Single">

    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly="true" Width="auto" Header="ID" Binding="{Binding id}"/>
        <DataGridTextColumn Width="*" Header="Bezeichnung" Binding="{Binding bezeichnung}"/>
        <DataGridTextColumn Width="*" Header="Typ" Binding="{Binding typ}"/>
        <DataGridTextColumn Width="*" Header="Nettopreis" Binding="{Binding nettopreis}"/>
    </DataGrid.Columns>

    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Margin" Value="0,0,0,5"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <StackPanel Background="#FF706F6F" DataContext="{Binding Items}">
                                    <DockPanel HorizontalAlignment="Stretch" Margin="0 5px 0 5px">
                                        <TextBlock Foreground="White" FontWeight="Bold" Margin="0 0 20px 0" TextWrapping="NoWrap">
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="{}{0} {1}">
                                                    <Binding Path="vorname" />
                                                    <Binding Path="nachname" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>                     
                                    </DockPanel>
                                    <ItemsPresenter />
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

CS:

dgErfasst.ItemsSource = mainController.loadErfasst();

CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(dgErfasst.ItemsSource);
cv.GroupDescriptions.Clear();
PropertyGroupDescription pgd = new PropertyGroupDescription("vorname"); //name
cv.GroupDescriptions.Add(pgd);
pgd = new PropertyGroupDescription("nachname"); //surname
cv.GroupDescriptions.Add(pgd);

如果我只添加一个组描述,那么布局几乎是完美的。但是第二组描述(“nachname”)变得混乱:

有什么想法可以修复布局吗?也许是不同的分组方式? 谢谢

【问题讨论】:

    标签: c# wpf xaml datagrid grouping


    【解决方案1】:

    如果“混乱”是指 Max Musterfrau 的重复结果集,并且您真的只希望组是 Name + Surname 的唯一集合(因此 Max Musterfrau、Max Smith 和 Johnny Smith 都是单独的单独组)... 您可以向 ViewModel 添加一个新属性,该属性将完整的唯一名称作为单个属性返回。然后为该属性添加一个 PropertyGroupDescription

    public string FullName { get { return vorname + " " + nachname; } }
    

    还有……

    CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(dgErfasst.ItemsSource);
    cv.GroupDescriptions.Clear();
    PropertyGroupDescription pgd = new PropertyGroupDescription("FullName");     //name
    cv.GroupDescriptions.Add(pgd);
    

    【讨论】:

    • 没问题,如果您需要其他问题,请提出另一个问题:)
    • 很高兴看到一个最近的相关 WPF 问题得到如此迅速的回答:)。
    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2013-09-30
    • 2013-10-29
    • 2013-12-27
    相关资源
    最近更新 更多