title: why my SelectedItems dependency property always returns null to bound property
body: <p>I created a UserControl1 that wraps a DataGrid (this is simplified for test purposes, the real scenario involves a third-party control but the issue is the same). The UserControl1 is used in the MainWindow of the test app like so:</p>
  
  <pre><code>&lt;test:UserControl1 ItemsSource="{Binding People,Mode=OneWay,ElementName=Self}"
                               SelectedItems="{Binding SelectedPeople, Mode=TwoWay, ElementName=Self}"/&gt;
  </code></pre>
  
  <p><strong>Everything works as expected except that when a row is selected in the DataGrid, the SelectedPeople property is always set to null.</strong> </p>
  
  <p>The row selection flow is roughly: UserControl1.DataGrid -> UserControl1.DataGrid_OnSelectionChanged -> UserControl1.SelectedItems -> MainWindow.SelectedPeople</p>
  
  <p>Debugging shows the IList with the selected item from the DataGrid is being passed to the SetValue call of the SelectedItems dependency property. But when the SelectedPeople setter is subsequently called (as part of the binding process) the value passed to it is always null.</p>
  
  <p>Here's the relevant UserControl1 XAML:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;DataGrid x:Name="dataGrid" SelectionChanged="DataGrid_OnSelectionChanged" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>In the code-behind of UserControl1 are the following definitions for the SelectedItems dependency properties and the DataGrid SelectionChanged handler:</p>
  
  <pre><code>    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(UserControl1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));
      public IList SelectedItems
      {
          get { return (IList)GetValue(SelectedItemsProperty); }
  
          set
          {
              SetValue(SelectedItemsProperty, value);
          }
      }
  
      private bool _isUpdatingSelectedItems;
  
      private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          var ctrl = d as UserControl1;
  
          if ((ctrl != null) &amp;&amp; !ctrl._isUpdatingSelectedItems)
          {
              ctrl._isUpdatingSelectedItems = true;
  
              try
              {
                  ctrl.dataGrid.SelectedItems.Clear();
                  var selectedItems = e.NewValue as IList;
  
                  if (selectedItems != null)
                  {
                      var validSelectedItems = selectedItems.Cast&lt;object&gt;().Where(item =&gt; ctrl.ItemsSource.Contains(item) &amp;&amp; !ctrl.dataGrid.SelectedItems.Contains(item)).ToList();
                      validSelectedItems.ForEach(item =&gt; ctrl.dataGrid.SelectedItems.Add(item));
                  }
              }
              finally
              {
                  ctrl._isUpdatingSelectedItems = false;
              }
          }
      }
  
      private void DataGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          if (!_isUpdatingSelectedItems &amp;&amp; sender is DataGrid)
          {
              _isUpdatingSelectedItems = true;
  
              try
              {
                  var x = dataGrid.SelectedItems;
                  SelectedItems = new List&lt;object&gt;(x.Cast&lt;object&gt;());
              }
              finally
              {
                  _isUpdatingSelectedItems = false;
              }
          }
      }
  </code></pre>
  
  <p>Here is definition of SomePeople from MainWindow code-behind:</p>
  
  <pre><code>    private ObservableCollection&lt;Person&gt; _selectedPeople;
      public ObservableCollection&lt;Person&gt; SelectedPeople
      {
          get { return _selectedPeople; }
          set { SetProperty(ref _selectedPeople, value); }
      }    
  
      public class Person
      {
          public Person(string first, string last)
          {
              First = first;
              Last = last;
          }
  
          public string First { get; set; }
          public string Last { get; set; }
      }
  </code></pre>