GetParentGroupFromItem Method (DataGridContext)
Retrieves the parent group for the specified item.
Parameters
- item
- The item whose parent group is to be retrieved.
Return Value
A CollectionViewGroup representing the parent group of the specified item. Can be a null reference (Nothing in Visual Basic) if the item is part of the context but does not have parent group.
All examples in this topic assume that the grid is bound to the Orders or Employees table of the Northwind database, unless stated otherwise.
The following example demonstrates how to handle the PreviewMouseLeftButtonDown event on the GroupHeaderControl objects contained in the headers of the child groups to toggle the expansion state of child groups using the ToggleGroupExpansion method. The group whose state is to be toggled will be retrieved using the GetParentGroupFromItem method.The following code provides the implementation of the PreviewMouseLeftButtonDown event in which we will retrieve the item represented by the GroupHeaderControl (GroupHeaderFooterItem) using the GetItemFromContainer method, which will then be used to retrieve the parent group (GetParentGroupFromItem) whose state is to be toggled.The following code provides the implementation of the PreviewMouseLeftButtonDown event in which we will retrieve the item represented by the GroupHeaderControl (GroupHeaderFooterItem) using the GetItemFromContainer method, which will then be used to retrieve the parent group (GetParentGroupFromItem) whose state is to be toggled.The following example demonstrates how to retrieve the parent group of the current item using the GetParentGroupFromItem method so that it can be collapsed. The implementation for the CollapseCurrentGroup method is provided below.The following provides the implementation for the CollapseCurrentGroup method.The following provides the implementation for the CollapseCurrentGroup method.
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current},
Path=Orders}">
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ShipCountry"/>
<xcdg:DataGridGroupDescription PropertyName="ShipCity"/>
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
<Style TargetType="{x:Type xcdg:GroupHeaderControl}">
<EventSetter Event="PreviewMouseLeftButtonDown"
Handler="HeaderDown"/>
</Style>
</Grid.Resources>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</Grid>
Private Sub HeaderDown( ByVal sender As Object, ByVal e As MouseEventArgs )
Dim headerControl As GroupHeaderControl = TryCast( sender, GroupHeaderControl );
If header Is Nothing Then
Return
End If
Dim context As DataGridContext = DataGridControl.GetDataGridContext( headerControl )
Dim item As Object = context.GetItemFromContainer( headerControl ) )
If Not item Is Nothing Then
Dim group As CollectionViewGroup = context.GetParentGroupFromItem( item )
If Not group Is Nothing
context.ToggleGroupExpansion( group )
End If
End If
End Sub
private void HeaderDown( object sender, MouseEventArgs e )
{
GroupHeaderControl headerControl = sender as GroupHeaderControl;
if( headerControl == null )
return;
DataGridContext context = DataGridControl.GetDataGridContext( headerControl );
object item = context.GetItemFromContainer( headerControl );
if( item != null )
{
CollectionViewGroup group = context.GetParentGroupFromItem( item );
if( group != null )
{
context.ToggleGroupExpansion( group );
}
}
}
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current},
Path=Orders}">
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ShipCountry"/>
<xcdg:DataGridGroupDescription PropertyName="ShipCity"/>
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
</Grid.Resources>
<DockPanel>
<Button Content="Collapse Group"
Click="CollapseCurrentGroup"
DockPanel.Dock="Top"/>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"
DockPanel.Dock="Bottom"/>
</DockPanel>
</Grid>
Private Sub CollapseCurrentGroup( ByVal sender As Object, ByVal e As RoutedEventArgs )
Dim context As DataGridContext = Me.OrdersGrid.CurrentContext
Dim group As CollectionViewGroup = context.GetParentGroupFromItem( context.CurrentItem )
context.CollapseGroup( group )
End Sub
private void CollapseCurrentGroup( object sender, RoutedEventArgs e )
{
DataGridContext context = this.OrdersGrid.CurrentContext;
CollectionViewGroup group = context.GetParentGroupFromItem( context.CurrentItem );
context.CollapseGroup( group );
}
.NET: net5.0, net5.0-windows, net6.0, net6.0-macos, net6.0-windows, net7.0, net7.0-macos, net7.0-windows, net8.0, net8.0-browser, net8.0-macos, net8.0-windows, net9.0, net9.0-browser, net9.0-macos, net9.0-windows, net10.0, net10.0-browser, net10.0-macos, net10.0-windows.
.NET Framework: net20, net35, net40, net403, net45, net451, net452, net46, net461, net462, net463, net47, net471, net472, net48, net481.