title: How to show properties of listbox selected item in different controls
body: <p>In my application I have a window which contain a <code>ListBox</code>, and controls that should show different properties of its currently selected item. Those controls are:</p>
  
  <ol>
  <li><code>TextBox</code> that should show 'Name' property.</li>
  <li><code>TextBox</code> that should show 'DataFile` property.</li>
  <li><code>DataGrid</code> that should show 'TItems property, which is an <code>ObservableCollection</code>.</li>
  </ol>
  
  <p>I tried to bind <code>SelectedItem</code> to an object, and then bind different properties of that object to the controls mentioned above, with no success.</p>
  
  <p>The window:</p>
  
  <p><a href="https://i.stack.imgur.com/q1s2T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q1s2T.png" alt="enter image description here"></a></p>
  
  <p>My View:</p>
  
  <pre><code>&lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:ReportMaker"
      xmlns:ViewModel="clr-namespace:ReportMaker.ViewModel" x:Class="ReportMaker.MainWindow"
      mc:Ignorable="d"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.DataContext&gt;
      &lt;ViewModel:MainViewModel/&gt;
  &lt;/Window.DataContext&gt;
  &lt;Grid&gt;
      &lt;Button x:Name="button" Content="Create" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75"/&gt;
      &lt;ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="120"/&gt;
      &lt;ListBox x:Name="listBox" HorizontalAlignment="Left" Margin="10,10,0,36.667" Width="119" ItemsSource="{Binding ReportItems}" &gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;TextBlock Text="{Binding Name}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  
      &lt;StackPanel HorizontalAlignment="Left" Height="274" Margin="134,10,0,0" VerticalAlignment="Top" Width="375" DataContext="{Binding SelectedReportItem}"&gt;
          &lt;StackPanel.Resources&gt;
              &lt;Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}"&gt;
                  &lt;Setter Property="Margin" Value="0, 10, 0, 0" /&gt;
              &lt;/Style&gt;
          &lt;/StackPanel.Resources&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="Name:"/&gt;
              &lt;TextBox Width="150" Text="{Binding Name}"/&gt;
          &lt;/StackPanel&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="Data File:"/&gt;
              &lt;TextBox Width="150" Text="{Binding ID}"/&gt;
          &lt;/StackPanel&gt;
          &lt;DataGrid Height="190" VerticalAlignment="Bottom" ItemsSource="{Binding TItems}"/&gt;
      &lt;/StackPanel&gt;
      &lt;Button x:Name="button_Copy" Content="Save" HorizontalAlignment="Right" Margin="0,0,92,10" VerticalAlignment="Bottom" Width="75"/&gt;
  
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>My ViewModel:</p>
  
  <pre><code>public class MainViewModel
  {
      public ObservableCollection&lt;ReportItem&gt; ReportItems { get; set; }
      public object SelectedReportItem { get; set; }
      public MainViewModel()
      {
          ReportItems = new ObservableCollection&lt;ReportItem&gt;();
          ReportItems.Add(Example);
      }
      public ReportItem Example = new TextReportItem() { Name = "John", DataFile = "try.txt"};
  }
  </code></pre>
  
  <p>ReportItem:</p>
  
  <pre><code>public class ReportItem
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string DataFile { get; set; }
  }
  </code></pre>
  
  <p>TextReportItem:</p>
  
  <pre><code>public class TextReportItem : ReportItem
  {
      public ObservableCollection&lt;TextParcel&gt; TItems { get; set; }
  }
  public class TextParcel
  {
      char Delimiter { get; set; }
      string LineExp { get; set; }
      string Result { get; set; }
      string IgnoreLine { get; set; }
      int DesiredResultIndexInLine { get; set; }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> as I use MVVM, I prefer to use only XAML in the View, with no code behind.</p>
  
  <p><strong>EDIT 2:</strong> </p>
  
  <p>Thanks to S.Akbari I succeeded to view the desired properties in the <code>TextBox</code> controls, with the following code:</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="Name:"/&gt;
              &lt;TextBox Width="150" Text="{Binding ElementName=listBox, Path=SelectedItem.Name}"/&gt;
          &lt;/StackPanel&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="Data File:"/&gt;
              &lt;TextBox Width="150" Text="{Binding ElementName=listBox, Path=SelectedItem.DataFile}"/&gt;
          &lt;/StackPanel&gt;
  </code></pre>
  
  <p>But when the same logic is applied to my <code>DataGrid</code>, it fails for some reason:</p>
  
  <pre><code>&lt;DataGrid Height="190" VerticalAlignment="Bottom" ItemsSource="{Binding ElementName=listBox, Path=SelectedItem.TItmes}" /&gt;
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>&lt;DataGrid Height="190" VerticalAlignment="Bottom" DataContext="{Binding ElementName=listBox, Path=SelectedItem}" ItemsSource="{Binding TItems}"/&gt;
  </code></pre>
  
  <p>And also:</p>
  
  <pre><code>&lt;DataGrid Height="190" VerticalAlignment="Bottom" DataContext="{Binding ElementName=listBox, Path=SelectedItem}"&gt;
              &lt;DataTemplate&gt;
                  &lt;TextBlock Text="{Binding TItems}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/DataGrid&gt;
  </code></pre>