title: How do I make my WPF User Control's dependency properties update my view model?
body: <p>I'm trying to create a user control with dependency properties to bind to. Internally I have a <code>ComboBox</code> that is bound to these same properties, but the binding only works one way. The <code>ComboBox</code> fills from the <code>ItemsSource</code>, but <code>SelectedItem</code> doesn't get updated back to the viewmodel I'm binding to.</p>
  
  <p>A simplified example:</p>
  
  <p>This is the view model to bind with the user control:</p>
  
  <pre><code>public class PeopleViewModel : INotifyPropertyChanged
  {
      public PeopleViewModel()
      {
          People = new List&lt;string&gt;( new [] {"John", "Alfred","Dave"});
          SelectedPerson = People.FirstOrDefault();
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private IEnumerable&lt;string&gt; _people;
      public IEnumerable&lt;string&gt; People 
      { 
          get { return _people; } 
          set
          {
              _people = value;
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs("People"));
              }
          }
      }
  
      private string _selectedPerson;
      public string SelectedPerson 
      {
          get { return _selectedPerson; }
          set 
          { 
              _selectedPerson = value;
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs("SelectedPerson"));
              }
          } 
      }
  }
  </code></pre>
  
  <p>This is the User control:</p>
  
  <pre><code>&lt;UserControl x:Class="PeopleControlTest.PeopleControl"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" d:DesignHeight="56" d:DesignWidth="637"&gt;
  &lt;StackPanel &gt;
      &lt;ComboBox Margin="11"
                ItemsSource="{Binding BoundPeople, RelativeSource={RelativeSource AncestorType=UserControl}}"
                SelectedItem="{Binding BoundSelectedPerson, RelativeSource={RelativeSource AncestorType=UserControl}}"/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p></p>
  
  <p>with code behind</p>
  
  <pre><code>public partial class PeopleControl : UserControl
  {
      public PeopleControl()
      {
          InitializeComponent();
      }
  
      public static readonly DependencyProperty BoundPeopleProperty =
          DependencyProperty.Register("BoundPeople", typeof(IEnumerable&lt;string&gt;), typeof(PeopleControl), new UIPropertyMetadata(null));
  
      public static readonly DependencyProperty BoundSelectedPersonProperty =
          DependencyProperty.Register("BoundSelectedPerson", typeof(string), typeof(PeopleControl), new UIPropertyMetadata(""));
  
      public IEnumerable&lt;string&gt; BoundPeople
      {
          get { return (IEnumerable&lt;string&gt;)GetValue(BoundPeopleProperty); }
          set { SetValue(BoundPeopleProperty, value); }
      }
  
      public string BoundSelectedPerson
      {
          get { return (string)GetValue(BoundSelectedPersonProperty); }
          set { SetValue(BoundSelectedPersonProperty, value); }
      }
  }
  </code></pre>
  
  <p>And this is how I bind the user control in the main window (with the windows data context set to an instance of the viewmodel)</p>
  
  <pre><code>&lt;Window x:Class="PeopleControlTest.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:controls="clr-namespace:PeopleControlTest"
      Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;controls:PeopleControl 
              BoundPeople="{Binding People}" 
              BoundSelectedPerson="{Binding SelectedPerson}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The combobox in the user control fills with the names, but when I select a different name this doesn't get updated back to the view model. Any idea what I'm missing here?</p>
  
  <p>Thanks!</p>