1

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

    }
4

1 回答 1

0

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

testTakerGrid.ItemSource = view;
于 2012-11-22T10:28:40.780 回答