Пример #1
0
        public static void SetHasBindableSelectedItems(System.Windows.Controls.ListBox source, bool value)
        {
            var handler = (SelectionChangedHandler)source.GetValue(SelectionChangedHandlerProperty);

            if (value && handler == null)
            {
                handler = new SelectionChangedHandler(source);
                source.SetValue(SelectionChangedHandlerProperty, handler);

            }
            else if (!value && handler != null)
                source.ClearValue(SelectionChangedHandlerProperty);
        }
Пример #2
0
            private void Initialize(System.Windows.Controls.ComboBox 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 });
                }
            }