title: WPF two way binding not working on CheckBox and List<string>
body: <p>Even after reviewing a multitude of solution proposals I cannot get a simple two way binding in xaml to work. I have a Window, a dataContext and an App. Problem is that:</p>
  
  <p>a) while the App constructor runs, the Window (initialized and <code>.Show</code>-ed in the same constructor) shows up, but is not updated at all, even though I toggle the checkbox value in my C# code a couple times;</p>
  
  <p>b) when the App constructor finishes, the Window is updated exactly once; I have it set up so that if I click the checkbox in the Window, the event handler in App (bound to the DataContext property change notification) should increase the size of a list of strings, which is also displayed. The increase of the list happens correctly in the code, but is not reflected in the Window.</p>
  
  <p><strong>Summary</strong>:</p>
  
  <ul>
  <li>user input in the Window reach the App's C# code fine: I can act on check box changes etc.</li>
  <li>the opposite direction does not work: whenever items are changed in the dataContext via code, the Window is not automatically updated, even though iNotifyProperty is implemented and executed.</li>
  </ul>
  
  <p><strong>What I would expect is that</strong>:</p>
  
  <p>a) while the App constructor runs and toggles the CheckBox value, the Window should reflect the changes by setting / clearing the tick on the box;</p>
  
  <p>b) after the App constructor finishes, whenever I toggle the CheckBox from FALSE to TRUE, the <code>NameList</code> is appended with a new string. I would expect the list in the Window to increase accordingly and automatically show the complete, appended <code>NameList</code> contents.</p>
  
  <p><strong>Observations</strong>:</p>
  
  <ul>
  <li>I try to ensure that the DataContext on the Window is set before calling <code>InitializeComponent</code> on the Window. Does not really make a difference unfortunately ...</li>
  <li><strong>I get a single clue in VS in the MainWindow.xaml file</strong>: the CheckBox Path as well as the ListBox Binding <code>NameList</code> are annotated with
  <code>Cannot resolve symbol due to unknown DataContext</code>
  However, when the App constructor terminates the Window is updated and when I click the CheckBox, the correct NotifyProperty event is triggered. This tells me that the runtime bindings actually should work ... apparently only one-way, not two-way though.</li>
  </ul>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window x:Class="StatisticsEvaluation.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;StackPanel Orientation="Vertical"&gt;
  
          &lt;CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Content="CheckBox" /&gt;
  
          &lt;ListBox ItemsSource="{Binding NameList, Mode=TwoWay}"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock Text="{Binding}"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
  
          &lt;TextBlock FontSize="18" FontFamily="Arial" Foreground="Black" Text="TextBlock" Visibility="Visible" /&gt;
  
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindow.xaml.cs:</p>
  
  <pre><code>namespace StatisticsEvaluation
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      public MainWindow()
      {            
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The App and DataContext:</p>
  
  <pre><code>namespace StatisticsEvaluation
  {
      public class DataContextClass : INotifyPropertyChanged
      {
          private bool isChecked;
  
          public bool IsChecked
          {
              get
              {
                  return isChecked;
              }
  
              set
              {
                  isChecked = value;
                  OnPropertyChanged("IsChecked");
              }
          }
  
          private List&lt;string&gt; nameList;
  
          public List&lt;string&gt; NameList
          {
              get
              {
                  return nameList;
              }
  
              set
              {
                  nameList = value;
                  OnPropertyChanged("NameList");
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          public void OnPropertyChanged(string propertyName)
          {
              var handler = PropertyChanged;
              if (handler != null) 
                  handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
  
      /// &lt;summary&gt;
      /// Interaction logic for App.xaml
      /// &lt;/summary&gt;
  
      public partial class App : Application
      {
          private MainWindow MyWindow { get; set; }
  
          private DataContextClass MyDataContext{ get; set; }
  
          private void HandleDataContextPropertyChange(object sender, PropertyChangedEventArgs e)
          {
              // If the CheckBox was just toggled to TRUE, increase the NameList
              // with an additional name and call OnPropertyChanged on it ... 
              // hoping that this would trigger a Window UI update - but no luck !
  
              if ((e.PropertyName == "IsChecked") &amp;&amp; MyDataContext.IsChecked)
              {
                  var randomProvider = new Random();
                  MyDataContext.NameList.Add(randomProvider.Next().ToString());
                  MyDataContext.OnPropertyChanged("NameList");
              }
          }
  
          public App()
          {
              MyDataContext = new DataContextClass();
              MyDataContext.PropertyChanged += HandleDataContextPropertyChange;
  
              MyWindow = new MainWindow {DataContext = MyDataContext};
              MyWindow.InitializeComponent();
              MyWindow.Show();
  
              MyDataContext.NameList = new List&lt;string&gt;();
              MyDataContext.NameList.Add("FirstName");
              MyDataContext.NameList.Add("SecondName");
              MyDataContext.NameList.Add("ThirdName");
  
              MyDataContext.IsChecked = true;
              Thread.Sleep(3000);
              MyDataContext.IsChecked = false;
              Thread.Sleep(3000);
              MyDataContext.IsChecked = true;
          }       
      }
  }
  </code></pre>
  
  <p>When I start the App the following Window appears, once the App constructor hits <code>.Show</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/bycGO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bycGO.png" alt="enter image description here"></a></p>
  
  <p>Once the App contructor has finished, the Window is updated once, but never again afterwards, regardless how many strings are added to <code>NameList</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/BGo5q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BGo5q.png" alt="enter image description here"></a></p>
  
  <p>Any ideas, why my two way binding only works in one direction ?</p>