title: Binding Nested Observable Collection in xaml using c# WPF
body: <p>I am having a hard time trying to figure out how to bind in xaml a nested Observable Collection.  The PLC class contains Tags.  This might be familiar if you work in Automation.  I have marked the areas of code I am having trouble with by saying &quot;<em><strong>!Can't Figure this out</strong></em>&quot;.  I am new to xaml and trying to do the binding in the xaml.  If it can't be done in the xaml, a code behind solution would be helpful.</p>
  <p><strong>PLC Class</strong></p>
  <pre><code>public class PLC
  {
      public string Name { get; set; }
      public ObservableCollection&lt;Tag&gt; Tags { get; set; }
      public PLC(string name)
      {
          Name = name;
          Tags = new ObservableCollection&lt;Tag&gt;();
      }
      public override string ToString()
      {
          return Name;
      }
  }
  </code></pre>
  <p><strong>Tag Class</strong> The PLC's tags when you click on a PLC  the ListView to the right will get the tags associated with that PLC.</p>
  <pre><code>public class Tag
  {
      public Tag(string name, int value)
      {
          Name = name;
          Value = value;
      }
      public string Name { get; set; }
      public int Value { get; set; }
  }
  </code></pre>
  <p><strong>xaml</strong> - note this is a user control binded to the parent's viewmodel.</p>
  <pre><code>&lt;UserControl x:Class=&quot;Test.UserControls.RuntimeControl&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
               mc:Ignorable=&quot;d&quot; 
               d:DesignHeight=&quot;450&quot; d:DesignWidth=&quot;800&quot;&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height=&quot;20&quot;&gt;&lt;/RowDefinition&gt;
              &lt;RowDefinition Height=&quot;*&quot;&gt;&lt;/RowDefinition&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid Grid.Row=&quot;0&quot;&gt;
              &lt;TextBlock Foreground=&quot;Red&quot; Margin=&quot;10,0,0,0&quot; &gt;Runtime&lt;/TextBlock&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Row=&quot;1&quot;&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width=&quot;*&quot;&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition Width=&quot;*&quot;&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;ListView x:Name=&quot;PLCLV&quot; Grid.Column=&quot;0&quot; Margin=&quot;10&quot; FontSize=&quot;25&quot; SelectionMode=&quot;Single&quot; ScrollViewer.VerticalScrollBarVisibility=&quot;Visible&quot;
                        BorderThickness=&quot;0&quot; ItemsSource=&quot;{Binding PLCs}&quot;&gt;
                  &lt;ListView.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text=&quot;{Binding Name}&quot;&gt;&lt;/TextBlock&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListView.ItemTemplate&gt;
              &lt;/ListView&gt;
              &lt;ListView Grid.Column=&quot;1&quot; Margin=&quot;10&quot; FontSize=&quot;25&quot; AlternationCount=&quot;2&quot; ScrollViewer.VerticalScrollBarVisibility=&quot;Visible&quot;
                        BorderThickness=&quot;0 &quot; ItemsSource=***!Can't Figure this out!***&gt;
                  &lt;ListView.ItemContainerStyle&gt;
                      &lt;Style TargetType=&quot;ListViewItem&quot;&gt;
                          &lt;Setter Property=&quot;Focusable&quot; Value=&quot;false&quot;/&gt;
                      &lt;/Style&gt;
                  &lt;/ListView.ItemContainerStyle&gt;
                  &lt;ListView.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock&gt;
                              &lt;Run Text = &quot;Name: &quot;/&gt;
                              &lt;Run Text =***!Can't Figure this out!***
                              &lt;Run Text =&quot;Value: &quot;/&gt;
                              &lt;Run Text =***!Can't Figure this out!***
                          &lt;/TextBlock&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListView.ItemTemplate&gt;
              &lt;/ListView&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  <p><strong>FIX in the UserControl xaml Answer Marked as accepted</strong></p>
  <pre><code>&lt;ListView Grid.Column=&quot;1&quot; Margin=&quot;10&quot; FontSize=&quot;25&quot; 
  DataContext=&quot;{Binding SelectedItem, ElementName=PLCLV}&quot; 
  ItemsSource=&quot;{Binding Tags}&quot;&gt;
  </code></pre>