title: Binding to Child Class of DataContext
body: <p>I have a WPF Window that has a <strong>list of tasks</strong> on the left, and <strong>details about the selected task</strong> on the right. The tasks is just a collection of task, you can consider it POCO.</p>
  
  <p><strong>Single ViewModel having both Tasks and the selected Task</strong></p>
  
  <pre><code>public class TaskViewModel
  {
      public TaskViewModel()
      {
          Tasks = new Task().GetTasks();
      }
  
  
      // A LIST OF MANY TASKS
      private Tasks _tasksViewModel;
      public Tasks Tasks
      {
          get { return _tasksViewModel; }
          private set
          {
              _tasksViewModel= value;
          }
      }
  
  
      // A SINGLE SELECTED TASK
      private static readonly DependencyProperty CurrentTaskProperty = DependencyProperty.Register(
          "CurrentTask", typeof(Task), typeof(TaskViewModel), new PropertyMetadata(null));
      public Task CurrentTask
      {
          get { return (Task) GetValue(CurrentTaskProperty); }
          set { SetValue(CurrentTaskProperty, value); }
      }
  }
  </code></pre>
  
  <p><strong>Window in XAML:</strong></p>
  
  <pre><code>&lt;Window&gt;
      &lt;!-- A List with multiple tasks. --&gt;
      &lt;ListBox x:Name="listBoxTasks" ItemsSource="{Binding Path=Task, Mode=TwoWay} /&gt;
  
      &lt;!-- The List's selected task's "Task Name" property. --&gt;
      &lt;TextBox x:Name="textBoxTaskName" Text="{Binding Path=CurrentTask, Mode=TwoWay}" /&gt;    
  
      &lt;!-- The List's selected tasks' "Task Items" child objects. --&gt;
      &lt;ListBox x:Name="listBoxTaskItems" ItemsSource="{Binding Path=EditableValue, Mode=TwoWay}" /&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>And my MainWindow xaml codebehind:</strong></p>
  
  <pre><code>public partial class MainWindow : Window
  {
      private MainWindow()
      {
          DataContext = _tasksViewModel;
      }
  
      private void OnListBoxTasksSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          _tasksViewModel.CurrentTask = task;
          DataContext = _tasksViewModel;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>And now the questions:</p>
  
  <p><strong>How many view models should I have?</strong></p>
  
  <ul>
  <li>Should I add a second ViewModel? Should I have one ViewModel for Tasks and one for the selected Task, or should I have just one ViewModel for the whole window?</li>
  </ul>
  
  <p><strong>And what is the best way to bind in XAML, and to set the data context(s) in C#?</strong></p>
  
  <ul>
  <li><p>How can I have only one DataContext for the whole Window? If the Window's DataContext is the TasksViewModel, how can I bind something to TasksViewModel.CurrentTask? Is there a way to bind to a property of a child item of Window.DataContext, or do I need to manually specify a separate DataContext for any controls that use CurrentTask?</p>
  
  <ul>
  <li><strong>Is there a way to do it like this:</strong></li>
  </ul></li>
  </ul>
  
  <p>In C#:</p>
  
  <pre><code>DataContext = _tasksViewModel.Tasks
  </code></pre>
  
  <p>In XAML:</p>
  
  <pre><code>&lt;!-- Can I do something similar to DataContext.CurrentTask.TaskName ??? --&gt;
  &lt;TextBox x:Name="textBoxTaskName" MinWidth="100" Text="{Binding Path=DataContext.CurrentTask.TaskName, Mode=TwoWay}" /&gt;
  
  &lt;!-- What about DataContext.CurrentTask.TaskItems??? --&gt;
  &lt;ListBox x:Name="listBoxTaskItems" ItemsSource="{Binding Path=**DataContext.CurrentTask.TaskItems**, DisplayMember="TaskItemName}" /&gt;
  </code></pre>
  
  <p><strong>Or in other words, is there a better solution than to do this:</strong></p>
  
  <pre><code> // When a Task in Tasks is selected...
  OnListBoxTasksSelectionChanged()
  {
      [...]
  
      // Set that Task as the DataContext for other controls
      textBoxTaskName.DataContext = Task;
  
      listBoxTaskItems.DataContext = Task.TaskItems;
   }
  </code></pre>