title: Data Binding to Nested Properties?
body: <p>I'm pretty new to WPF and XAML and now I'm stuck with data binding for days! I just wanted to bind some nested properties to a TextBox and ListView (via XAML), but I'm doing it wrong.
  Here's my Sample Code:</p>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>namespace CounterTestNestedDataBinding
  {
      public partial class MainWindow : Window
      {
          public MyModel MyModel { get; set; }
  
          public MainWindow()
          {
              InitializeComponent();
              MyModel = new MyModel { MyCounter = new Counter() };
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              MyModel.MyCounter.incrementCounter();
          }
      }
  }
  </code></pre>
  
  <p><strong>MyModel.cs</strong></p>
  
  <pre><code>namespace CounterTestNestedDataBinding
  {
      public class MyModel : INotifyPropertyChanged
      {
          public Counter _myCounter;
          public Counter MyCounter
          {
              get { return _myCounter; }
              set
              {
                  _myCounter = value;
                  NotifyPropertyChanged("MyCounter");
              }
          }
  
          // some other members and properties ...
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void NotifyPropertyChanged(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Counter.cs</strong></p>
  
  <pre><code>namespace CounterTestNestedDataBinding
  {
      public class Counter : INotifyPropertyChanged
      {
          #region Members
          private int _currentNumber;
          private ObservableCollection&lt;int&gt; _historyList;
          #endregion
  
          #region Constructor
          public Counter()
          {
              _currentNumber = 0;
              _historyList = new ObservableCollection&lt;int&gt;();
          }
          #endregion
  
          #region Properties
          public int CurrentNumber
          {
              get { return _currentNumber; }
              set
              {
                  _currentNumber = value;
                  NotifyPropertyChanged("CurrentNumber");
              }
          }
  
          public ObservableCollection&lt;int&gt; HistoryList
          {
              get { return _historyList; }
              set
              {
                  _historyList = value;
                  NotifyPropertyChanged("HistoryList");
              }
          }
          #endregion
  
          public void incrementCounter()
          {
              HistoryList.Add(CurrentNumber);
              CurrentNumber++;
          }
  
          public override string ToString()
          {
              return string.Format("CurrentNumber: {0}, HistoryList: {1}", _currentNumber, String.Join(",", _historyList));
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void NotifyPropertyChanged(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="CounterTestNestedDataBinding.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:counterTestNestedDataBinding="clr-namespace:CounterTestNestedDataBinding"
          Title="MainWindow" Height="350" Width="200" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
          DataContext="{Binding RelativeSource={RelativeSource Self}}"
          &gt;
      &lt;StackPanel Orientation="Vertical"&gt;
          &lt;TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyModel.MyCounter.CurrentNumber}"/&gt;
          &lt;Button Content="Button" Click="Button_Click"/&gt;
          &lt;ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyModel.MyCounter.HistoryList}"&gt;&lt;/ListView&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li><p>How can I bind nested properties? Is it possible? Why does something like</p>
  
  <pre><code>Text="{Binding MyModel.MyCounter.CurrentNumber}"
  </code></pre>
  
  <p>not work? </p></li>
  <li><p>Is the "DataContext" set correct in the XAML?</p></li>
  </ol>