I try to group some data in wpf datagrid .. it works fine, except the HEADER OF TEMPLATE DOESN'T DISPLAY any thing. It should display teacher name by TEACHER property. I use SQtOLinq as the underlayer data source. what did I miss in my code?
XAML code:
<DataGrid AutoGenerateColumns="False" Height="311" Style="{StaticResource DashboardGridStyle}" HorizontalAlignment="Left" Name="TeacherDetailsDG" VerticalAlignment="Top" Width="322" ItemsSource="{Binding}" Margin="178,0,0,0" SelectionChanged="TeacherDetailsDG_SelectionChanged" RowBackground="#FF00E700" SelectionUnit="FullRow" BorderBrush="#FF00E400" AlternatingRowBackground="#FFC4B5B5">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel >
<TextBlock Text="{Binding Path=Teacher,Mode=TwoWay}" Foreground="Blue"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Teacher,Mode=TwoWay}" />
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter Visibility="{Binding ElementName=exp, Path=IsExpanded}" />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=TeacherID}" Visibility="Hidden">
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Teacher}"/>
<!--Grades column-->
<DataGridComboBoxColumn x:Name="GradesCombo" ItemsSource="{Binding}" DisplayMemberPath="Grade" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=GradeID}"></DataGridComboBoxColumn>
<!--Subjects column-->
<DataGridComboBoxColumn x:Name="SubjectsCombo" ItemsSource="{Binding}" DisplayMemberPath="Subject" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=SubjectID}"></DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
Datagrid Binding code:
public MainWindow()
{
InitializeComponent();
GradesCombo.ItemsSource = SchoolDC.GradesTables;
SubjectsCombo.ItemsSource = SchoolDC.SubjectsTables;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView((SchoolDC.TeachersDetailsSP().ToList<object>()));
view.GroupDescriptions.Add(new PropertyGroupDescription("Teacher"));
TeacherDetailsDG.ItemsSource = view;
}
catch (Exception)
{
throw;
}
}
This is the result:
GroupItem.Name gets the value of the property you grouped by .
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" Foreground="Blue"/>
</DataTemplate>