title: DataGrid in a TreeView in WPF
body: <p>I'm trying to bind to a collection of <code>StringValue</code> objects in the <code>DataGrid</code> below. But each item is displayed in separate row. </p>
  
  <p>I want it to have a columnar display. For example the values 565477 and 65656 should be displayed in one row. </p>
  
  <p><a href="https://i.stack.imgur.com/5TM7f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5TM7f.png" alt="enter image description here"></a></p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;TreeView&gt;
      &lt;TreeViewItem Header="Warnings"&gt;
          &lt;TreeView ItemsSource="{Binding WarningCategories}"&gt;
              &lt;TreeView.ItemTemplate&gt;
                  &lt;HierarchicalDataTemplate ItemsSource="{Binding Path=Warnings}"&gt;
                      &lt;TextBlock Text="{Binding Path=Category}" /&gt;
                      &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;DataGrid ItemsSource="{Binding Fields}" AutoGenerateColumns="True" HeadersVisibility="None"&gt;
                              &lt;/DataGrid&gt;                                
                          &lt;/DataTemplate&gt;
                      &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/TreeView.ItemTemplate&gt;
          &lt;/TreeView&gt;
      &lt;/TreeViewItem&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p><strong>Code-behind:</strong></p>
  
  <pre><code>   public class ViewModel : INotifyPropertyChanged
     {
        public event PropertyChangedEventHandler PropertyChanged;
  
        public ObservableCollection&lt;WarningCategory&gt; WarningCategories { get; set; }
  
  
        public ViewModel()
        {
           WarningCategories = new ObservableCollection&lt;WarningCategory&gt;();
  
           ObservableCollection&lt;Warning&gt; warnings = new ObservableCollection&lt;Warning&gt;
           {
              new Warning( new StringValue("565477"), new StringValue("65656")),
              new Warning( new StringValue("767455"), new StringValue("75642")),
           };
  
  
           WarningCategories.Add(new WarningCategory("Warning One", warnings));
  
  
  
           warnings = new ObservableCollection&lt;Warning&gt;
           {
              new Warning( new StringValue("565477"), new StringValue("65656")),
              new Warning( new StringValue("767455"), new StringValue("75642")),
           };
  
  
  
           WarningCategories.Add(new WarningCategory("Warning Two", warnings));
        }
  
  
  
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
           var handler = PropertyChanged;
           if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
     }
  
     public class WarningCategory
     {
        public string Category { get; set; }
  
        private ObservableCollection&lt;Warning&gt; m_warnings;
  
        public ObservableCollection&lt;Warning&gt; Warnings
  
        {
           get
           {
              return m_warnings ?? (m_warnings = new ObservableCollection&lt;Warning&gt;());
           }
        }
  
  
        public WarningCategory(string category, ObservableCollection&lt;Warning&gt; animals)
  
        {
           Category = category;
  
           m_warnings = animals;
        }
     }
  
     public class Warning
  
     {
        public ObservableCollection&lt;StringValue&gt; Fields { get; set; }
  
        public Warning(params StringValue[] fields)
  
        {
           Fields = new ObservableCollection&lt;StringValue&gt;(fields);
        }
     }
  
     public class StringValue
     {
        public StringValue(string s)
        {
           Value = s;
        }
        public string Value { get; set; }
     }
  </code></pre>
  
  <p><strong>I want the final result to be something like this:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/0J8pR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0J8pR.png" alt="enter image description here"></a></p>
  
  <p><strong>My workaround so far, but unfortunately it does not support multiselection!</strong></p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Fields}"&gt;
      &lt;ItemsControl.ItemsPanel&gt;
          &lt;ItemsPanelTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"/&gt;
          &lt;/ItemsPanelTemplate&gt;
      &lt;/ItemsControl.ItemsPanel&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock Text="{Binding Value}" Width="150"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>