private void Initialize(CB comboBox, bool useEagerSelection) { // Add to visual tree to enable ElementName binding comboBox.Tag = this._bindingListener; this._useEagerSelection = useEagerSelection; BindingExpression be = comboBox.GetBindingExpression(ItemsControl.ItemsSourceProperty); if (be != null) { BindingOperations.SetBinding(this._bindingListener, BindingListener.ItemsSourceProperty, be.ParentBinding); BindingOperations.SetBinding(comboBox, ItemsControl.ItemsSourceProperty, new Binding("Items") { Source = this }); } be = comboBox.GetBindingExpression(Selector.SelectedItemProperty); if (be != null) { BindingOperations.SetBinding(this._bindingListener, BindingListener.SelectedItemProperty, be.ParentBinding); BindingOperations.SetBinding(comboBox, Selector.SelectedItemProperty, new Binding("SelectedItem") { Source = this, Mode = BindingMode.TwoWay }); } be = comboBox.GetBindingExpression(Selector.SelectedValueProperty); if (be != null) { // We'll always bind the ComboBox to SelectedItem, but we'll map it to the existing SelectedValue binding this._selectedValueMode = true; BindingOperations.SetBinding(this._bindingListener, BindingListener.SelectedValueProperty, be.ParentBinding); comboBox.ClearValue(Selector.SelectedValueProperty); // Bind SelectedValuePath be = comboBox.GetBindingExpression(Selector.SelectedValuePathProperty); if (be != null) { BindingOperations.SetBinding(this._bindingListener, BindingListener.SelectedValuePathProperty, be.ParentBinding); comboBox.ClearValue(Selector.SelectedValuePathProperty); } else { this._bindingListener.SelectedValuePath = comboBox.SelectedValuePath; comboBox.SelectedValuePath = null; } // Bind DisplayMemberPath be = comboBox.GetBindingExpression(Selector.DisplayMemberPathProperty); if (be != null) { BindingOperations.SetBinding(this._bindingListener, BindingListener.DisplayMemberPathProperty, be.ParentBinding); } else { this._bindingListener.DisplayMemberPath = comboBox.DisplayMemberPath; } BindingOperations.SetBinding(comboBox, Selector.DisplayMemberPathProperty, new Binding("DisplayMemberPath") { Source = this }); // Check selection mode and path properties if (this._useEagerSelection && (this._bindingListener.DisplayMemberPath != this._bindingListener.SelectedValuePath)) { throw new InvalidOperationException("Cannot use eager selection when the DisplayMemberPath and the SelectedValuePath differ. Try using basic ComboBoxMode.Async selection instead."); } BindingOperations.SetBinding(comboBox, Selector.SelectedItemProperty, new Binding("SelectedItem") { Source = this, Mode = BindingMode.TwoWay }); } }