【问题标题】:How can I Group Data in WPF Toolkid Datagrid That is Bound to a Datatable如何在绑定到数据表的 WPF Toolkid Datagrid 中对数据进行分组
【发布时间】:2012-11-22 09:34:26
【问题描述】:

我是 wpf 的新手。我正在.net 3.5 中使用 wpf 开发一个小型应用程序。我有一个与数据表绑定的 wpf toolkid 数据网格控件。现在我无法在我的数据网格上对数据进行分组。

谁能告诉我如何做到这一点?

这是我的xmal

        <Window.Resources>
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander x:Name="exp" IsExpanded="True"
                                  Background="White"
                                  Foreground="Black">
                                    <Expander.Header>
                                        <TextBlock Text="{Binding AdmissionDate}"/>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Window.Resources>

<tk:DataGrid Grid.Row="2" Grid.ColumnSpan="4" Margin="5,0,5,0"
                     AutoGenerateColumns="False" 
                     Name="testTakerGrid" 
                     AlternationCount="2" 
                     AlternatingRowBackground="Azure"
                     RowHeaderWidth="40"
                     CanUserSortColumns="True"
                     IsSynchronizedWithCurrentItem="True"
                     GridLinesVisibility="None"
                     ItemsSource="{Binding}"
                     SelectionMode="Single"
                     CanUserAddRows="False" CanUserDeleteRows="False">
         <tk:DataGrid.GroupStyle>
                        <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <tk:DataGridRowsPresenter/>
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </tk:DataGrid.GroupStyle>
<tk:DataGrid.Columns>                
        <tk:DataGridTextColumn Header="Serial Id" Binding="{Binding Path=SerialId, Mode=OneWay, ValidatesOnDataErrors=True}" Width="60"/>              
        <tk:DataGridTextColumn Header="Name" Binding="{Binding Path=Name, Mode=OneWay, ValidatesOnExceptions=True}" Width="200"/>
        <tk:DataGridTextColumn Header="Gender" Binding="{Binding Path=Gender, Mode=OneWay, ValidatesOnExceptions=True}"/>
        <tk:DataGridTextColumn Header="Admission Date" Binding="{Binding Path=AdmissionDate, Mode=OneWay, ValidatesOnExceptions=True, StringFormat='d'}" Width="100" />
</tk:DataGrid.Columns>
</tk:DataGrid>

这是我的隐藏代码

    public partial class MainApp : Window
    {
        TestTakerDataSet takerDs = new TestTakerDataSet();
        TestTakersTableAdapter takerTa = new TestTakersTableAdapter();

        CollectionView view;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
                 takerTa.FillByTerm(takerDs.TestTakers, "2010-2011T1E");        

                        view = (CollectionView)CollectionViewSource.GetDefaultView(takerDs.TestTakers);
this.DataContext=view;
        }

    }

【问题讨论】:

    标签: wpf datatable wpftoolkit


    【解决方案1】:

    @sovantha 在您上面给出的代码中,我无法在任何地方看到数据网格绑定。在 XAML 中使用资源绑定或在后面的代码中绑定 Datagrid ItemSource,如下所示:

    testTakerGrid.ItemSource = view;
    

    【讨论】:

    • 我已经设置了 datagrid 的 ItemsSource="{Binding}" 并且在我的代码后面我有 this.DataContext = takerDs.TestTakers;网格按预期显示数据。
    • 尝试将 DataContext 与视图绑定,因为 CollectionView 中启用了分组。
    • 我将代码更改为:this.DataContext=view;网格正常显示,但不出现分组。
    • 您需要在 CollectionView 的代码文件中应用分组。 view.GroupDescriptions.Add(new PropertyGroupDescription("AdmissionDate"));为了更好地理解,请访问以下站点,它准确地解释了您想要实现的目标。 msdn.microsoft.com/en-us/library/ff407126.aspx
    • 当您为数据网格指定组样式时,它是该组的标题的样式。例如在上面提到的链接中,Group header 是在 Expander 中定义的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多