title: XAML => I can't access Datagrid parent from Group child
body: <p>I like to Binding the datagrid parent from Behavior (pnlDgSubFooter) placed in DataGrid/GroupStyle/ContainerStyle/Style[GroupItem]/Setter[template]/ControlTemplate/Expander/Header/DockPanel/StackPanel
  But the Binding can't leave of DockPanel.
  in ElementName, there is only "btnExpandAll" and himself.
  In Behavior, the LeDataGrid property return alway null (except if i bind on . or btnExpandAll)</p>
  
  <pre><code>&lt;DataGrid Grid.Row="0" x:Name="dgMain" AutoGenerateColumns="False" SelectionUnit="FullRow" LoadingRow="dgMain_LoadingRow" MouseDown="dgMain_MouseDown" Sorting="dgMain_Sorting"
      CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserResizeRows="False" CanUserSortColumns="True" CanUserAddRows="False"
      Style="{StaticResource dg}" RowStyle="{StaticResource dgRow}" CellStyle="{StaticResource dgCell}" ColumnHeaderStyle="{StaticResource dgColHeader}" RowHeaderStyle="{StaticResource dgRowHeader}"
      ItemsSource="{Binding NotifyOnSourceUpdated=True, Source={StaticResource cvsElmts}}" HorizontalAlignment="Left" &gt;
  &lt;!--DataGrid.DataContext&gt;&lt;Binding Source="{StaticResource tblUsers}"/&gt;&lt;/DataGrid.DataContext--&gt;
  &lt;i:Interaction.Triggers&gt;
      &lt;i:EventTrigger EventName="SelectionChanged"&gt;
          &lt;mvvm:EventToCommand Command="{Binding SendCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=dgMain}" PassEventArgsToCommand="False"/&gt;
      &lt;/i:EventTrigger&gt;
  &lt;/i:Interaction.Triggers&gt;
  &lt;DataGrid.GroupStyle&gt;
      &lt;GroupStyle&gt;
      &lt;!-- Style for groups under the top level. --&gt;
          &lt;GroupStyle.ContainerStyle&gt;
              &lt;Style TargetType="{x:Type GroupItem}"&gt;
                  &lt;Setter Property="Margin" Value="0,0,0,5"/&gt;
                  &lt;Setter Property="Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt;
                              &lt;Expander IsExpanded="{Binding DataContext.ExpandedAll, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" BorderThickness="1,1,1,5"&gt;
                                  &lt;Expander.Style&gt;
                                      &lt;Style TargetType="{x:Type Expander}"&gt;
                                          &lt;Style.Triggers&gt;
                                              &lt;DataTrigger Binding="{Binding IsBottomLevel}" Value="True"&gt;
                                                  &lt;Setter Property="Margin" Value="8,0,0,0" /&gt;
                                              &lt;/DataTrigger&gt;
                                          &lt;/Style.Triggers&gt;
                                      &lt;/Style&gt;
                                  &lt;/Expander.Style&gt;
                                  &lt;Expander.Header&gt;
                                      &lt;DockPanel&gt;
                                          &lt;Button Name="btnExpandAll" Command="{Binding DataContext.ExpandedAllCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Click="btnExpandAll_Click" ToolTip="{x:Static resx:resMain.lblExpandAll}" BorderThickness="0"&gt;
                                              &lt;TextBlock Grid.Column="0" Text="&amp;#xE155;" FontFamily="{StaticResource FntSymbol}" Foreground="{StaticResource scbBlack}" FontSize="12" VerticalAlignment="Center" HorizontalAlignment="Center"/&gt;
                                          &lt;/Button&gt;
                                          &lt;TextBlock FontWeight="Bold" Text="" Margin="5,0,0,0"&gt;&lt;Run Text="{Binding Path=Name, Mode=OneWay}" /&gt;&lt;Run Text=" ("/&gt;&lt;Run Text="{Binding Path=ItemCount, Mode=OneWay}" /&gt;&lt;Run Text=" éléments)."/&gt;&lt;/TextBlock&gt;
                                          &lt;StackPanel Name="pnlDgSubFooter" HorizontalAlignment="Left" Orientation="Horizontal" &gt;
                                              &lt;i:Interaction.Behaviors&gt;
                                                  &lt;Classes:BehaviorWithCommand LeDataGrid="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" LeGroupe="{Binding .}" /&gt;
                                              &lt;/i:Interaction.Behaviors&gt;
                                          &lt;/StackPanel&gt;
                                      &lt;/DockPanel&gt;
                                  &lt;/Expander.Header&gt;
                                  &lt;Expander.Content&gt;
                                      &lt;Border BorderThickness="1" BorderBrush="{StaticResource scbGrey3}"&gt;
                                      &lt;ItemsPresenter /&gt;
                                      &lt;/Border&gt;
                                  &lt;/Expander.Content&gt;
                              &lt;/Expander&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/GroupStyle.ContainerStyle&gt;
  ....
  </code></pre>
  
  <p>The Behavior:</p>
  
  <pre><code>public class BehaviorWithCommand : Behavior&lt;FrameworkElement&gt; { // StackPanel
      public static readonly DependencyProperty LeDataGridProperty = DependencyProperty.Register(nameof(LeDataGrid), typeof(object), typeof(BehaviorWithCommand), new PropertyMetadata(null));
  
      public static readonly DependencyProperty LeGroupeProperty = DependencyProperty.Register(nameof(LeGroupe), typeof(object), typeof(BehaviorWithCommand), new PropertyMetadata(null));
  
      public object LeDataGrid {
          get { return (object)GetValue(LeDataGridProperty); }
          set { SetValue(LeDataGridProperty, value); }
      }
  
      public object LeGroupe {
          get { return (object)GetValue(LeGroupeProperty); }
          set { SetValue(LeGroupeProperty, value); }
      }
  
      protected override void OnAttached() {
          base.OnAttached();
          ((Panel)AssociatedObject).Children.Clear();
          var dg = new DataGrid();
          dg.Columns.Add(new DataGridTextColumn());
          dg.Columns.Add(new DataGridTextColumn());
          dg.Columns.Add(new DataGridTextColumn());
          //var dg = (DataGrid)LeDataGrid;
          foreach (var item in dg.Columns) {
              var g = new Grid() { MinWidth = 10 };
              g.SetBinding(Grid.WidthProperty, new System.Windows.Data.Binding("ActualWidth") { Source = item }); // dhHeadName DataGridColumn
              var t = new TextBox() { Margin = new Thickness(1, 0, 1, 0), Background = new SolidColorBrush(Color.FromRgb(200, 200, 200)), FontWeight = FontWeights.Bold, FontSize = 9, IsReadOnly = true };
              t.Text = "Coucou";
              g.Children.Add(t);
              ((Panel)AssociatedObject).Children.Add(g);
          }
      }
  
      protected override void OnDetaching() {
          base.OnDetaching();
      }
  
  }
  </code></pre>