Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:21:35+00:00 2026-05-17T23:21:35+00:00

I need to apply grouping to rows in a DataGrid that is nested in

  • 0

I need to apply grouping to rows in a DataGrid that is nested in a TabControl. Both the DataGrid and the TabControl are databound. The outermost control is bound to a ViewModel that exposes a collection of pages that is bound to the TabControl. Each page exposes a collection of lines that is bound to the Grid.

I am tried to follow patterns like this one from SO and this one from C-SharpCorner. I’m not married to these, so if there is a better pattern (as this post seems to indicate), I’m willing to go in another direction.

For now, I am not sure where to inject the definition of the PropertyGroupDescription and bind it to my desired PropertyName. I’ve tried doing it as a resource and directly as a CollectionViewSource on the DataGrid. But neither produced reasonable results.

This is what I have so far:

<UserControl x:Class="View.ViewInvoiceUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" >
<Grid Name="_grid" MinHeight="100">

snipped for clarity…

<TabControl 
    ItemsSource="{Binding Path=CurrentInvoice.InvoicePages}" 
    Grid.Column="0" 
    Grid.ColumnSpan="4" 
    Grid.Row="5" 
    HorizontalAlignment="Left" 
    Margin="0,4,0,0" 
    Name="_invoicePageTabControl" 
    VerticalAlignment="Top">
    <TabControl.ItemContainerStyle />
        <TabControl.ContentTemplate>
            <DataTemplate>
                <DataGrid 
                    Name="_invoiceLineGrid" 
                    AutoGenerateColumns="False" 
                    CanUserSortColumns="True"
                    ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
                    HorizontalAlignment="Left" 
                    ItemsSource="{Binding InvoiceLines}" 
                    IsReadOnly="True"
                    RowDetailsVisibilityMode="VisibleWhenSelected"
                    RowStyle="{StaticResource DataGridRowStyle}"
                    SelectionUnit="FullRow"
                    VerticalAlignment="Top"
                    Visibility="{Binding Path=InvoiceLineGridVisibility}"
                    Initialized="_invoiceLineGrid_Initialized">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Line Number" MinWidth="50"  Binding="{Binding InvoiceLineNumber}" />
                        <DataGridTextColumn Header="Description" MinWidth="50" Width="*" Binding="{Binding Description}" />
                        <DataGridTextColumn Header="Quantity" MinWidth="50" Binding="{Binding Quantity}" />
                        <DataGridTextColumn Header="Extended Cost" MinWidth="50" Width="*" Binding="{Binding ExtendedCost}" />
                    </DataGrid.Columns>
                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander>
                                                    <Expander.Header>
                                                        <StackPanel>
                                                            <TextBlock Text="{Binding Name}" />
                                                        </StackPanel>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>
                </DataGrid>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>
</UserControl>

I think I need to add something like:

<CollectionViewSource>
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="DepartmentBillingCode" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

If I add the CollectionViewSource as a resource and change the DataGrid binding, there are no rows in the grid.
If I add the CollectionViewSource to the DataGrid, I get an exception: Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead. nested in Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.
But I don’t see a GroupDescription or PropertyGroupDescription on DataGrid.ItemSource

So, I’m feeling lost now.
Any suggestions are appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:21:35+00:00Added an answer on May 17, 2026 at 11:21 pm

    So I found a possible answer. I’m open to other people’s interpretation. Is this a good solution?

    Applying an SO post regarding what part of MVVM pattern is resonsible for grouping of DataGrid, I went in search of a solution to generate a grouping in the ViewModel. I found a simple example on WpfTutorial.com

    In my InvoicePageViewModel, I created a new property that creates a ListViewCollection from my List. ListViewCollection allows me to add my own GroupDescription as follows:

        public ListCollectionView GroupedInvoiceLines
        {
            get
            {
                ListCollectionView groupedLines = new ListCollectionView(InvoiceLines); // TODO should this be cached?
    
                groupedLines.GroupDescriptions.Add(new PropertyGroupDescription("DepartmentBillingCode"));
    
                return groupedLines;
            }
        }
    

    Then it was trivial to change the data binding on the DataGrid from InvoiceLines to GroupedInvoiceLines.

    Comments? Questions?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to apply validation on input time intervals that are taken in as
I need to apply a class to the post archive links that are output
I need to apply a jQuery plugin to an HTML element that will be
I need to apply a check so that a user cannot register using an
I need to apply this to images that are selected by jQuery selector for
I have a value in Jquery. I need to apply that value to a
I have a value in Jquery. I need to apply that value to a
I have an XML file that I need to apply a namespace to at
I need to apply AJAX loader that loads some different contents form the page,
Please I need to apply the toggle() function to only li that has ul

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.