title: How the RowDetails do not show when selecting several datagird's rows in wpf
body: <p>I used RowDetails in Datagrid for show my table .My DataGrid xml code is :</p>
  
  <pre><code>    &lt;Controls:MetroWindow.Resources&gt;
       &lt;DataTemplate x:Key="RowDetailTemplate" x:Name="rowTemplate"&gt;
          &lt;Grid x:Name="RowDetailGrid"
                Width="750"
                Height="Auto"
                Margin="5"&gt;
  
              &lt;Border HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      CornerRadius="5"&gt;
  
                  &lt;Grid&gt;
                      &lt;ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto"&gt;
                          &lt;DataGrid 
                                 x:Name="dginfo"
  
                            HeadersVisibility="All" 
                           Margin="2,0,2,2"
                           GridLinesVisibility="All"
                            AutoGenerateColumns="False"
                            CanUserAddRows="False"
                            CanUserReorderColumns="False"
                            CanUserResizeColumns="False"
                            CanUserResizeRows="False"
                            EnableColumnVirtualization="True"
                            EnableRowVirtualization="True"
                            IsReadOnly="True"
                             ScrollViewer.CanContentScroll="True" 
  
  
                            SelectionUnit="FullRow"
                            TextOptions.TextFormattingMode="Display" LoadingRow="MetroDataGrid_LoadingRow" Width="700" FlowDirection="RightToLeft" Height="auto" Loaded="dginfo_Loaded"&gt;
  
                              &lt;DataGrid.Columns&gt;
                                  &lt;DataGridTextColumn Header="مقدار" Binding="{Binding m1}" Width="*"&gt;
                                      &lt;DataGridTextColumn.ElementStyle&gt;
                                          &lt;Style&gt;
                                              &lt;Setter Property="TextBlock.TextWrapping" Value="Wrap" /&gt;
                                              &lt;Setter Property="TextBlock.TextAlignment" Value="Left"/&gt;
                                          &lt;/Style&gt;
                                      &lt;/DataGridTextColumn.ElementStyle&gt;
                                  &lt;/DataGridTextColumn&gt;
                              &lt;/DataGrid.Columns&gt;
                              &lt;DataGrid.ColumnHeaderStyle&gt;
                                  &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
                                      &lt;Setter Property="FontWeight"  Value="Bold" /&gt;
                                      &lt;Setter Property="Height"  Value="auto" /&gt;
                                      &lt;Setter Property="ContentTemplate"&gt;
                                          &lt;Setter.Value&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;TextBlock TextWrapping="Wrap" Text="{Binding}"&gt;&lt;/TextBlock&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/Setter.Value&gt;
                                      &lt;/Setter&gt;
                                      &lt;Setter Property="TextBlock.TextAlignment" Value="Center" /&gt;
                                  &lt;/Style&gt;
                              &lt;/DataGrid.ColumnHeaderStyle&gt;
                              &lt;DataGrid.RowHeaderTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;TextBlock Text="{Binding DataContext.m0, 
                      RelativeSource={RelativeSource AncestorType=DataGridRow}}"&gt;&lt;/TextBlock&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/DataGrid.RowHeaderTemplate&gt;
                              &lt;DataGrid.RowHeaderStyle&gt;
                                  &lt;Style TargetType="{x:Type DataGridRowHeader}"&gt;
                                      &lt;Setter Property="Width" Value="100"/&gt;
                                      &lt;Setter Property="Foreground" Value="Black"/&gt;
                                      &lt;!--&lt;Setter Property="Background" Value="CornflowerBlue" /&gt;--&gt;
                                  &lt;/Style&gt;
                              &lt;/DataGrid.RowHeaderStyle&gt;
                          &lt;/DataGrid&gt;
                      &lt;/ScrollViewer&gt;
                  &lt;/Grid&gt;
              &lt;/Border&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  &lt;/Controls:MetroWindow.Resources&gt;    
                            &lt;DataGrid x:Name="MetroDataGrid"
                                  HeadersVisibility="All" 
                                  Margin="2,0,2,2"
                                  AutoGenerateColumns="False"
                                  CanUserReorderColumns="False"
                                  CanUserResizeColumns="False"
                                  CanUserResizeRows="False"
                                  GridLinesVisibility="All"
                                  EnableColumnVirtualization="True"
                                  EnableRowVirtualization="True"
                                  IsReadOnly="True"
                                  RowDetailsTemplate="{StaticResource RowDetailTemplate}"
                                  RowDetailsVisibilityChanged="dataGrid1_RowDetailsVisibilityChanged"
                                  RenderOptions.ClearTypeHint="Enabled"
                                  SelectionUnit="FullRow"
                                  Loaded="dataGrid1_Loaded"
                                  TextOptions.TextFormattingMode="Display" LoadingRow="MetroDataGrid_LoadingRow" SelectedCellsChanged="MetroDataGrid_SelectedCellsChanged"&gt;
                                  &lt;DataGrid.Columns&gt;
                                      &lt;DataGridTemplateColumn Header="تصویر" Width="Auto" IsReadOnly="True"&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;Image Source="{Binding Pics}" Width="40" Height="40" /&gt;
                                              &lt;/DataTemplate&gt;
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                                      &lt;/DataGridTemplateColumn&gt;
  
                                      &lt;DataGridTextColumn Binding="{Binding KalaID}" Header="شناسه "  Width="3*" FrameworkElement.FlowDirection="RightToLeft"/&gt;
                                      &lt;DataGridTextColumn Binding="{Binding KalaName}" Header="نام"  Width="3*" FrameworkElement.FlowDirection="RightToLeft"&gt;
                                          &lt;DataGridTextColumn.ElementStyle&gt;
                                              &lt;Style&gt;
                                                  &lt;Setter Property="TextBlock.TextWrapping" Value="Wrap" /&gt;
                                                  &lt;Setter Property="TextBlock.TextAlignment" Value="Left"/&gt;
  
                                              &lt;/Style&gt;
                                          &lt;/DataGridTextColumn.ElementStyle&gt;
  
                                      &lt;/DataGridTextColumn&gt;
                                      &lt;DataGridTextColumn Binding="{Binding catName}" Header="دسته بندی "  Width="3*" FrameworkElement.FlowDirection="RightToLeft"/&gt;
                                      &lt;DataGridTextColumn Binding="{Binding proCode}" Header="Product Code "  Width="3*" FrameworkElement.FlowDirection="RightToLeft"/&gt;
  
                                      &lt;DataGridTextColumn Binding="{Binding KalaTedad}" Header="تعداد"  Width="3*" FrameworkElement.FlowDirection="RightToLeft"/&gt;
  
                                      &lt;DataGridTextColumn  Binding="{Binding KalaPrice}" Header="قیمت واحد " Width="3*" FrameworkElement.FlowDirection="RightToLeft"&gt;
                                          &lt;DataGridTextColumn.ElementStyle&gt;
                                              &lt;Style&gt;
                                                  &lt;Setter Property="TextBlock.TextWrapping" Value="Wrap" /&gt;
                                                  &lt;Setter Property="TextBlock.TextAlignment" Value="Left"/&gt;
  
                                              &lt;/Style&gt;
                                          &lt;/DataGridTextColumn.ElementStyle&gt;
  
                                      &lt;/DataGridTextColumn&gt;
                                      &lt;DataGridTextColumn Binding="{Binding KalaMinTozihat}" Header="توضیحات" Width="3*" FrameworkElement.FlowDirection="RightToLeft"/&gt;
  
                                      &lt;DataGridTextColumn Binding="{Binding KalaDate, StringFormat=yyyy/MM/dd}" Header="تاریخ ثبت"  Width="3*" FrameworkElement.FlowDirection="RightToLeft"/&gt;
                                  &lt;/DataGrid.Columns&gt;
                                  &lt;DataGrid.ColumnHeaderStyle&gt;
                                      &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
                                          &lt;Setter Property="FontWeight"  Value="Bold" /&gt;
  
                                      &lt;/Style&gt;
                                  &lt;/DataGrid.ColumnHeaderStyle&gt;
                                  &lt;DataGrid.ContextMenu&gt;
                                      &lt;ContextMenu&gt;
                                          &lt;MenuItem Header="Edit" Click="MenuItem_Click" /&gt;
                                          &lt;MenuItem Header="Delete" Click="Context_Delete"/&gt;
  
                                      &lt;/ContextMenu&gt;
                                  &lt;/DataGrid.ContextMenu&gt;
                                  &lt;DataGrid.RowHeaderStyle&gt;
                                      &lt;Style TargetType="{x:Type DataGridRowHeader}"&gt;
                                          &lt;Setter Property="Width" Value="35"/&gt;
                                          &lt;Setter Property="Foreground" Value="Black"/&gt;
                                      &lt;/Style&gt;
                                  &lt;/DataGrid.RowHeaderStyle&gt;
                                  &lt;DataGrid.RowHeaderTemplate&gt;
                                      &lt;DataTemplate&gt;
                                          &lt;ToggleButton x:Name="RowHeaderToggleButton"
                                              Click="ToggleButton_Click"
                                              Cursor="Hand" /&gt;
                                      &lt;/DataTemplate&gt;
                                  &lt;/DataGrid.RowHeaderTemplate&gt;
                              &lt;/DataGrid&gt;
  </code></pre>
  
  <p>And methods are :</p>
  
  <pre><code>     private void ToggleButton_Click(object sender, RoutedEventArgs e)
      {
  
          try
          {
  
              // the original source is what was clicked.  For example 
              // a button.
              DependencyObject dep = (DependencyObject)e.OriginalSource;
  
              // iteratively traverse the visual tree upwards looking for
              // the clicked row.
              while ((dep != null) &amp;&amp; !(dep is DataGridRow))
              {
                  dep = VisualTreeHelper.GetParent(dep);
              }
  
              // if we found the clicked row
              if (dep != null &amp;&amp; dep is DataGridRow)
              {
                  DataGridRow row = (DataGridRow)dep;
                  KalaClass krow = (KalaClass)row.Item;
                  if (krow != null)
                  {
                      IQueryable&lt;product&gt; q = from p in entity.Products
                                              where p.productID == krow.KalaID
                                              select p;
  
                      curentProduct = q.First();
                  }
                  if (MetroDataGrid.SelectedItems.Count &gt; 1)
                  {
                      row.DetailsVisibility = Visibility.Collapsed;
                      return;
                  }
                  // change the details visibility
                  if (row.DetailsVisibility == Visibility.Collapsed)
                  {
                      row.DetailsVisibility = Visibility.Visible;
                  }
                  else
                  {
                      row.DetailsVisibility = Visibility.Collapsed;
                  }
              }
              //dgRowProFill(curentProduct);
          }
          catch (System.Exception)
          {
          }
      }
  
      private void dataGrid1_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
      {
  
          try
          {
              DataGridRow row = e.Row as DataGridRow;
              if (MetroDataGrid.SelectedItems.Count &gt; 1)
              {
  
                  return;
              }
              dg.Visibility = Visibility.Visible;
              KalaClass krow = (KalaClass)MetroDataGrid.SelectedItem;
  
              if (krow != null)
                  curentProduct = findProductByid(krow.KalaID);
  
              FrameworkElement tb = GetTemplateChildByName(row, "RowHeaderToggleButton");
              if (tb != null)
              {
  
                  if (row.DetailsVisibility == System.Windows.Visibility.Visible)
                  {
                      (tb as ToggleButton).IsChecked = true;
                  }
                  else
                  {
                      (tb as ToggleButton).IsChecked = false;
                  }
              }
              // dginfo_Loaded(dg);
              //dgRowProFill(curentProduct);
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message.ToString());
          }
  
      }
      public static FrameworkElement GetTemplateChildByName(DependencyObject parent, string name)
      {
  
  
          int childnum = VisualTreeHelper.GetChildrenCount(parent);
  
          for (int i = 0; i &lt; childnum; i++)
          {
              var child = VisualTreeHelper.GetChild(parent, i);
              if (child is FrameworkElement &amp;&amp;
  
                      ((FrameworkElement)child).Name == name)
              {
                  return child as FrameworkElement;
              }
              else
              {
                  var s = GetTemplateChildByName(child, name);
                  if (s != null)
                      return s;
              }
          }
          return null;
      }
  
      private void MetroDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
      {
  
          try
          {
              KalaClass krow = (KalaClass)MetroDataGrid.SelectedItem;
              if (krow != null)
              {
                  IQueryable&lt;product&gt; q = from p in entity.Products
                                          where p.productID == krow.KalaID
                                          select p;
  
                  curentProduct = q.First();
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message.ToString());
          }
  
  
      }
  </code></pre>
  
  <p>It's work succesfully like below :</p>
  
  <p><a href="https://i.stack.imgur.com/4iu4I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4iu4I.png" alt="enter image description here"></a></p>
  
  <p>When I selected several rows :</p>
  
  <p><a href="https://i.stack.imgur.com/QR1Ge.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QR1Ge.png" alt="enter image description here"></a></p>
  
  <p>But I want doesn't show RowDetails when I selecting several rows and Doesn't load inner Datagird. How can I fix it ? </p>