protected override DependencyObject GetContainerForItemOverride() { ComboBoxItem cbi = new ComboBoxItem(); // Bind using the "Enabled" property on the Animal class Binding enabledBinding = new Binding("IsEnabled"); enabledBinding.Mode = BindingMode.OneWay; // Bind it to the IsEnabledProperty of the ComboBoxItem cbi.SetBinding(ComboBoxItem.IsEnabledProperty, enabledBinding); return cbi; }
private void SetSelectedItem() { var selectedcbbItem = this.Items.FirstOrDefault(item => { ComboBoxItem cbbItem = item as ComboBoxItem; return cbbItem.DataContext == this.SelectedItem; }); if (selectedcbbItem == null) { ComboBoxItem cbbItemHidden = new ComboBoxItem(); cbbItemHidden.Visibility = System.Windows.Visibility.Collapsed; System.Windows.Data.Binding binding = new System.Windows.Data.Binding(); binding.Path = new PropertyPath(this.DisplayMemberPath); cbbItemHidden.SetBinding(ComboBoxItem.ContentProperty, binding); cbbItemHidden.DataContext = this.SelectedItem; this.Items.Add(cbbItemHidden); selectedcbbItem = cbbItemHidden; } base.SelectedItem = selectedcbbItem; }