如何在绑定到数据表的WPF Toolkit Datagrid中对数据进行分组

时间:2012-11-22 09:34:27

标签: wpf datatable wpftoolkit

我是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>

这是我的代码behide

    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;
        }

    }

1 个答案:

答案 0 :(得分:0)

@sovantha在上面给出的代码中,我无法看到datagrid绑定的任何地方。要么在XAML中使用Resource绑定,要么在Code中绑定Datagrid ItemSource,如下所示:

testTakerGrid.ItemSource = view;