【问题标题】:DataGrid Grouping in XAML with Data from SQL Table使用 SQL 表中的数据在 XAML 中进行 DataGrid 分组
【发布时间】:2019-10-28 09:36:34
【问题描述】:

我有一个 SQL 表名称 Cities 有四个字段: 城市 省 名称 代码 我想在 WPF DataGrid 上显示其数据并在其上按省设置分组

这是我的 XAML 设计

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:SLTADataSet x:Key="SLTADataSet"/>
        <CollectionViewSource x:Key="CountriesViewSource" Source="{Binding Countries, Source={StaticResource SLTADataSet}}"/>
        <CollectionViewSource x:Key="CitiesViewSource" Source="{Binding Cities, Source={StaticResource SLTADataSet}}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Province"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

    </Window.Resources>
    <Grid >
        <DataGrid x:Name="CitiesDataGrid"  ItemsSource="{Binding Source={StaticResource CitiesViewSource}}" CanUserAddRows="False" >
            <DataGrid.GroupStyle>
                <!-- Style for groups at top level. -->
                <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}">
                                        <Expander IsExpanded="false" Background="Gray" >
                                            <Expander.Header>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Source={StaticResource CitiesViewSource }, Path=Province}" Background="Yellow" />
                                            </Expander.Header>
                                            <Expander.Content>
                                                <ItemsPresenter />
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
                <!-- Style for groups under the top level. -->
                <!--<GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <DockPanel Background="yellow">
                                --><!--<TextBlock Text="{Binding Source={StaticResource CitiesViewSource}, Path=Province}" Foreground="Black"  Margin="30,0,0,0" Width="100"/>--><!--
                                <TextBlock Text="SSSSSSSSSSSSSS" Foreground="Black"  Margin="30,0,0,0" Width="100"/>
                            </DockPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>-->
            </DataGrid.GroupStyle>
        </DataGrid>
    </Grid>
</Window>

这是后面的代码

Class MainWindow

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
        Dim SLTADataSet As WpfApp1.SLTADataSet = CType(Me.FindResource("SLTADataSet"), WpfApp1.SLTADataSet)
        Dim SLTADataSetCitiesTableAdapter As WpfApp1.SLTADataSetTableAdapters.CitiesTableAdapter = New WpfApp1.SLTADataSetTableAdapters.CitiesTableAdapter()
        Dim CitiesViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("CitiesViewSource"), System.Windows.Data.CollectionViewSource)
        SLTADataSetCitiesTableAdapter.Fill(SLTADataSet.Cities)
        CitiesViewSource.View.MoveCurrentToFirst()
    End Sub

    Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized

    End Sub
End Class

分组已完成,工作完美 但我对 Datagrid 分组样式中的扩展器有疑问 对于所有组项目,它始终是表中第一个省份的名称,我找不到问题所在。 请您帮我解决一下。

【问题讨论】:

  • 您的问题不清楚。您希望在Expander 的标题中显示什么属性值?
  • @mm8: 请看一下 xaml 中扩展器的标题,我尝试将省份绑定到它,但它将显示数据网格中所有其他项目的第一个省份
  • 请看我的回答。

标签: wpf vb.net xaml datagrid grouping


【解决方案1】:

由于您已按Province 属性分组,您可以只绑定到GroupItem 的Name 属性:

<TextBlock FontWeight="Bold" Text="{Binding Name}" Background="Yellow" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多