【问题标题】:How To Grouping Data In WPF DataGrid如何在 WPF DataGrid 中对数据进行分组
【发布时间】:2017-06-21 13:17:54
【问题描述】:

在我的 WPF 应用程序中,我在这个数据网格中有数据网格,我想根据名为“City”的列名对数据库中的数据进行分组,我没有使用 mvvm 架构和列表方法,我正在使用 ICollectionView 并通过datatable对象作为参数,这是我的c#代码

ICollectionView cv = CollectionViewSource.GetDefaultView(dt);
                cv.GroupDescriptions.Add(newPropertyGroupDescription("City"));
                Hotels.ItemsSource = cv;

这是我的 XAML 代码:

<Window.Resources>
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <StackPanel>
                            <TextBlock Text="{Binding City}" Name="grouping" Foreground="Black"></TextBlock>
                            <ItemsPresenter/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

<DataGrid Background="Azure" ItemsSource="{Binding Hotels}" CanUserAddRows="False" Name="Hotels" Style="{StaticResource AzureDataGrid}" Margin="0,173,0,0">
            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource ResourceKey=GroupHeaderStyle}">
                    <GroupStyle.Panel>                        
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>

它不会在文本块中显示文本我正在将文本块与列名绑定,但它不起作用...请帮助我

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    如果您不为每个项目使用 viewmodel,而是使用 DataTable 的行对象,WPF 将无法识别名称“City”。您需要使用代表组项目的对象的CollectionViewGroupInternal.Name 属性持有的组名称。在这种特殊情况下,它是城市的名称。只需将 Name 绑定到您的 TextBlock。

    您可以使用组的 HeaderTemplate 以更少的代码行来实现您的目标。默认 ItemPresenter 将在 Header 下方可见。

    <!-- resources -->
    <CollectionViewSource x:Key="devicesCollection" IsLiveSortingRequested="True" IsLiveGroupingRequested="True" Source="{Binding MyCollection}">
        <!-- Sorting -->
        <CollectionViewSource.SortDescriptions>
            <componentModel:SortDescription PropertyName="Id" />
        </CollectionViewSource.SortDescriptions>
    
        <!-- Grouping -->
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="City"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
    
    <DataTemplate x:Key="GroupingHeader">
        <Border Background="Gray">
            <TextBlock Margin="10 5 5 5" FontSize="12" FontWeight="Bold" Text="{Binding Name}"/>
        </Border>
    </DataTemplate>
    
    <!-- data grid -->
    <DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=devicesCollection}, Mode=OneWay}">
        <DataGrid.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource ResourceKey=GroupingHeader}" />
        </DataGrid.GroupStyle>
    </DataGrid>
    

    【讨论】:

    • 我没有显示任何相同的结果 :(
    • 这可能是 CollectionViewSource 对象的问题。查看更新的答案。
    • 请粘贴视图模型。可能是错字什么的。
    • 'ICollectionView cv = CollectionViewSource.GetDefaultView(dt.AsDataView()) ;'这个?
    • 我错过了该信息,抱歉。将 Name 绑定到您的 TextBlock。答案已更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多