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 }); } }
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 }); } }
private UIElement createSizeType(int marginTop, string propName) { ComboBox comboBox = new ComboBox(); comboBox.Margin = new Thickness(0, marginTop, 0, 0); comboBox.Height = 28; comboBox.HorizontalAlignment = HorizontalAlignment.Left; comboBox.Name = propName + "textBox"; comboBox.VerticalAlignment = VerticalAlignment.Top; comboBox.Width = 120; comboBox.Items.Insert(0, "pix"); comboBox.Items.Insert(1, "per"); Binding binding = new Binding(propName); binding.Source = WhatsappProperties.Instance; binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit; comboBox.SetBinding(ComboBox.SelectedValueProperty, binding); BindingExpression be = comboBox.GetBindingExpression(ComboBox.SelectedValueProperty); BindingExpressions.Add(be); return comboBox; }