Bahaa Salaheldin
Bahaa Salaheldin

Reputation: 529

The group header is empty in Wpf Datagrid

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: enter image description here

Upvotes: 2

Views: 2419

Answers (2)

Mike Nakis
Mike Nakis

Reputation: 61986

If you are using ReSharper, (and you most definitely are, aren't you?) the problem with the accepted answer is that the reference to Name will be flagged with "Cannot resolve property 'Name' in data context of type 'Xyz'" where Xyz is the type of the enclosing data context, most probably the ViewModel of your screen.

(Or, your enclosing data context might also have a property called 'Name', in which case you will be oblivious to the discrepancy.)

To remedy this problem, change <DataTemplate> to <DataTemplate DataType="GroupItem"> to let WPF (and ReSharper) know what is the actual type of the data context that the DataTemplate is going to be applied to.

Then, the Name property will not be flagged anymore, and as a matter of fact, if you choose "Go to definition" on the property you will be taken to the Name property of UIElement, from which GroupItem inherits it. (Assuming that you are, of course, using ReSharper, instead of just plain woefully inadequate Visual Studio, right?)

Upvotes: 0

eran otzap
eran otzap

Reputation: 12533

GroupItem.Name gets the value of the property you grouped by .

     <DataTemplate>    
          <TextBlock Text="{Binding Path=Name}" Foreground="Blue"/>  
     </DataTemplate>

Upvotes: 4

Related Questions