title: WPF Custom DependencyProperty Binding - SetValue set correct value but binded property set receive null
body: <p>I have issue with custom DependencyProperty in my control. Let me explain:</p>
  
  <p>I have control with list of the checkable items. I need property binding to the IEnumerable SelectedItems. The logic of SelectedItemsProperty filling is inside of control, so it is not just simple binding. Here is code behind of my control:</p>
  
  <pre><code>    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IEnumerable&lt;object&gt;), typeof(ButtonColumnFilter));
  
      public IEnumerable&lt;object&gt; SelectedItems
      {
        get { return (IEnumerable&lt;object&gt;)GetValue(SelectedItemsProperty); }
        set { Debug.WriteLine("SelectedItems has been set in ButtonColumnFilter - Count:{0}", value?.Count());
              SetValue(SelectedItemsProperty, value);
            }
       }
  </code></pre>
  
  <p>Here I get Debug message with correct number of selected items, so my logic inside of control is working well. The property in XAML is binded here:</p>
  
  <pre><code>    &lt;MESControls:ButtonColumnFilter CanSearch="True" CanSort="False" DisplayMemberPath="DisplayName"
  ItemsSource="{Binding Path=FilterAdapter.Drivers, Mode=OneWay, IsAsync=True}"
  OnApplyFilter="Drivers_ApplyFilter"
  SelectedItems="{Binding Path=SelectedFilterDrivers, Mode=TwoWay}"/&gt;
  </code></pre>
  
  <p>And property is defined in my code here:</p>
  
  <pre><code>public IEnumerable&lt;Driver&gt; SelectedFilterDrivers
          {
              get =&gt; _SelectedFilterDrivers;
              set
              {
  
                  Debug.WriteLine("SelectedFilterDrivers has been set in PlannerFilterAdapter - Count: {0}", value?.Count());
                  if (_SelectedFilterDrivers != value)
                  {
  
                      _SelectedFilterDrivers = value;
                      NotifyPropertyChanged("SelectedFilterDrivers");
                  }
              }
          }
  </code></pre>
  
  <p>!!! But here I get Debug message 'value == null' !!!</p>
  
  <p>The binding is working well, and property set is called correctly in time i suppose. The weird is, that SetValue inside control has correct value, but outside in code the propert set value is null.</p>
  
  <p>What can be wrong?
  Thanks.</p>
  
  <p>UPDATE:</p>
  
  <p>Here is code that changes SelectedItems inside control:</p>
  
  <pre><code>private void CheckableItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
          {
  
              if (e.PropertyName == "IsChecked" &amp;&amp; !_IsChecking)
              {
                  _IsChecking = true;
  
                  CheckableItem item = sender as CheckableItem;
  
                  if(item.IsChecked == true &amp;&amp; InnerItemsSource.Where(ci=&gt;ci.Item.ToString() != SELECTALL).All(ci=&gt;ci.IsChecked == true))
                  {
                      InnerItemsSource.Single(ci =&gt; ci.Item.ToString() == SELECTALL).IsChecked = true;
                      SelectAllIsSelected = true;
                  }
                  else if (InnerItemsSource.Where(ci =&gt; ci.Item.ToString() != SELECTALL).All(ci=&gt;ci.IsChecked == true) || InnerItemsSource.Where(ci =&gt; ci.Item.ToString() != SELECTALL).All(ci =&gt; ci.IsChecked == false))
                  {
                      InnerItemsSource.Single(ci =&gt; ci.Item.ToString() == SELECTALL).IsChecked = false;
                      SelectAllIsSelected = false;
                  } 
                  else 
                  {
                      InnerItemsSource.Single(ci =&gt; ci.Item.ToString() == SELECTALL).IsChecked = null;
                      SelectAllIsSelected = false;
                  }
  
                  SelectedItems = CheckedItems;
  
                  NotifyPropertyChanged("IsFilterUsed");
                  NotifyPropertyChanged("FilterIcon");
  
  
              }
  
              _IsChecking = false;
          }
  
  public IEnumerable&lt;object&gt; CheckedItems
          {
              get
              {
                  return InnerItemsSource?.Where(ci =&gt; ci.IsChecked == true &amp;&amp; ci.Item.ToString() != SELECTALL).Select(ci =&gt; ci.Item);
              }
  
          }
  </code></pre>
  
  <p>But as I written, the property set inside control receive correct value and I guess correct type. The return value of CheckedItems is IEnumerable'&lt;<em>object</em>>'.</p>
  
  <p>Thanks for help.</p>
  
  <p><a href="https://i.stack.imgur.com/HGbUK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HGbUK.png" alt="value is IEnumerable&lt;Driver&gt;"></a></p>
  
  <p>Call stack situation</p>
  
  <p>Here is visible count of the collection just one step back in the call stack
  <a href="https://i.stack.imgur.com/hkQcV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hkQcV.png" alt="Here is visible count of the collection just one step back in the call stack"></a></p>
  
  <p>Next step of call stack - breakpoint - value is null
  <a href="https://i.stack.imgur.com/aLMkR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aLMkR.png" alt="Next step of call stack - breakpoint - value is null"></a>
  The true is, that between theses steps is some external code - but I didn't get any error neither warning
  <a href="https://i.stack.imgur.com/F1fLt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F1fLt.png" alt="The true is, that between theses steps is some external code - but I didn&#39;t get any error neither warning"></a></p>
  
  <p>UPDATE - some progress</p>
  
  <p>I tried change public IEnumerable&lt;<em>Driver</em>> SelectedFilterDrivers to </p>
  
  <pre><code>public IEnumerable&lt;object&gt; SelectedFilterDrivers
          {
              get =&gt; _SelectedFilterDrivers;
              set
              {
  
                  Debug.WriteLine("SelectedFilterDrivers has been set in PlannerFilterAdapter - Count: {0}", value?.Count());
                  if (_SelectedFilterDrivers != value)
                  {
  
                      _SelectedFilterDrivers = (IEnumerable&lt;Driver&gt;)value;
                      NotifyPropertyChanged("SelectedFilterDrivers");
                  }
              }
          }
  </code></pre>
  
  <p>And I receive correct number of items. So there is an issue during retyping of items in collection. So I need to kick forward with the better way, how to keep ability for SelectedItems to use generic IEnumerable&lt;<em>object</em>> </p>
  
  <p>Important is, that IEnumerable&lt;<em>object</em>> is working well with simple binding (in this case ItemsSource - true is, that it si just OneWay binding)</p>