Xceed DataGrid for WPF v7.3 Documentation
Xceed.Wpf.DataGrid Assembly / Xceed.Wpf.DataGrid Namespace / DataGridControl Class / GetDataGridContext Method
A DependencyObject representing the item whose DataGridContext is to be retrieved.
Example


In This Topic
    GetDataGridContext Method
    In This Topic
    Retrieves the DataGridContext for the specified DependencyObject.
    Syntax
    'Declaration
     
    Public Shared Function GetDataGridContext( _
       ByVal obj As DependencyObject _
    ) As DataGridContext
    'Usage
     
    Dim obj As DependencyObject
    Dim value As DataGridContext
     
    value = DataGridControl.GetDataGridContext(obj)
    public static DataGridContext GetDataGridContext( 
       DependencyObject obj
    )

    Parameters

    obj
    A DependencyObject representing the item whose DataGridContext is to be retrieved.

    Return Value

    A reference to the item's DataGridContext.
    Example
    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 child contexts of the master data items and collapse any expanded details using the CollapseDetail method.The next example provides the implementation of the button's Click event.The next example provides the implementation of the button's Click event.
    <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>
      <Grid.Resources>
         <xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
                                            Source="{Binding Source={x:Static Application.Current},
                                                             Path=Employees}"/>
     
      </Grid.Resources>
     
      <DockPanel>
         <Button Content="Collapse All Details"
                 Click="Button_Click"
                 DockPanel.Dock="Top"/>
         <xcdg:DataGridControl x:Name="EmployeesGrid"
                               ItemsSource="{Binding Source={StaticResource cvs_employees}}"
                               ItemsSourceName="Order Information"
                               AutoCreateDetailConfigurations="True"/>
      </DockPanel>
    </Grid>
    Private Sub Button_Click( ByVal sender As Object, ByVal e As RoutedEventArgs )
      DataGridContext rootContext = DataGridControl.GetDataGridContext( this.EmployeesGrid );
    
      Dim childContexts As New List( Of DataGridContext)( Me.EmployeesGrid.GetChildContexts() )
    
      Dim context As DataGridContext
      For Each context In childContexts
        context.ParentDataGridContext.CollapseDetails( context.ParentItem )
      Next context
    End Sub
    private void Button_Click( object sender, RoutedEventArgs e )
    {
     DataGridContext rootContext = DataGridControl.GetDataGridContext( this.EmployeesGrid );
    
     List<DataGridContext> childContexts = new List<DataGridContext>( this.EmployeesGrid.GetChildContexts() );
    
     foreach( DataGridContext context in childContexts )
     {
       context.ParentDataGridContext.CollapseDetails( context.ParentItem );
     }     
    }
    Requirements

    Target Platforms: Windows 11, Windows, 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also