title: Datacontext binding gets lost in a master detail scenario
body: <p>I try to build a Master/Detail scenario by using the DataGrid as master and a HexControl (ref: <a href="https://github.com/abbaye/WpfHexEditorControl" rel="nofollow noreferrer">https://github.com/abbaye/WpfHexEditorControl</a>) as its detail content.</p>
  
  <p>I have an <strong>ObservableCollection</strong> where the <strong>RowModel</strong> class contains the properties for the DataGrid and I have a <strong>HexEditorContent</strong> class within the <strong>RowModel</strong> class, so I can create the Master/Detail pattern when the user selects a row to show the data in a HexEditor control. New rows are added via a button. Additionally, I use the Prism Framework.</p>
  
  <p>The minimal example in code looks like this:</p>
  
  <p><strong>MainWindow.xaml</strong>:</p>
  
  <pre><code>&lt;Window x:Class="HexEditorTest.MainWindow"
          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:HexEditorTest"
          xmlns:hexEditor="clr-namespace:WpfHexaEditor;assembly=WPFHexaEditor"
          mc:Ignorable="d"
          Title="MainWindow" Height="450" Width="800"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="0.2*" /&gt;
              &lt;RowDefinition Height="1*" /&gt;
              &lt;RowDefinition Height="1*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Button Content="Add Row" Click="Button_Click" Width="100"/&gt;
          &lt;DataGrid x:Name="MainGrid" ItemsSource="{Binding Rows}" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="A" Binding="{Binding Acol}" /&gt;
                  &lt;DataGridTextColumn Header="B" Binding="{Binding Bcol}" /&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
          &lt;GroupBox Grid.Row="2" Header="Payload"&gt;
              &lt;hexEditor:HexEditor DataContext="{Binding ElementName=MainGrid, Path=SelectedItem.HexEditorContent}" Stream="{Binding HexByteStream}"/&gt;
          &lt;/GroupBox&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And its <strong>MainWindow.xaml.cs</strong>:</p>
  
  <pre><code> public partial class MainWindow : Window
      {
          ViewModel viewModel;
  
          public MainWindow()
          {
              InitializeComponent();
  
              viewModel = new ViewModel();
  
              DataContext = viewModel;
  
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
  
              viewModel.Rows.Add(new RowModel
              {
                  Acol = "c",
                  Bcol = "d",
                  HexEditorContent = new HexEditorContent { HexByteStream = new MemoryStream(Enumerable.Repeat((byte)0, 10).ToArray(), true) }
              });
          }
      }
  
      public class ViewModel
      {
          public ObservableCollection&lt;RowModel&gt; Rows { get; set; } = new ObservableCollection&lt;RowModel&gt;();
      }
  
      public class HexEditorContent : BindableBase
      {
          private MemoryStream memStream;
          public MemoryStream HexByteStream
          {
              get { return memStream; }
              set { SetProperty(ref memStream, value); }
          }
      }
  
      public class RowModel : BindableBase
      {
  
          private string aCol;
          public string Acol
          {
              get { return aCol; }
              set { SetProperty(ref aCol, value); }
          }
  
          private string bCol;
          public string Bcol
          {
              get { return bCol; }
              set { SetProperty(ref bCol, value); }
          }
  
          private HexEditorContent hexEditor;
          public HexEditorContent HexEditorContent
          {
              get { return hexEditor; }
              set { SetProperty(ref hexEditor, value); }
          }
  
      }
  </code></pre>
  
  <p>Now, when I click to add a new row, the new row appears with the default data set, like:</p>
  
  <p><a href="https://i.stack.imgur.com/COtun.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/COtun.png" alt="enter image description here"></a></p>
  
  <p>But, when I add another row and switch back to the first row, the input of the first row is gone:</p>
  
  <p><a href="https://i.stack.imgur.com/SXrCO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SXrCO.png" alt="enter image description here"></a></p>
  
  <p>What am I doing wrong here?</p>
  
  <p>Every time I switch, the data context seems to be lost and it does never appear again...</p>
  
  <p>EDIT:</p>
  
  <p>Here is the Debug View of the MemoryStream</p>
  
  <p><a href="https://i.stack.imgur.com/o59O6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o59O6.png" alt="enter image description here"></a></p>