private ComboBoxEditableWhenFocused CreateCardinalityComboBox(object businessObject, string bindingPropertyName, string isVisiblePropetyName, string isEnabledPropertyName) { var comboBox = new ComboBoxEditableWhenFocused { Style = (Style)FindResource(COMBOBOX_STYLE), ItemsSource = CardinalityList.Instance, DisplayMemberPath = "Text", SelectedValuePath = "Value" }; SetCommunBindings(comboBox, ComboBoxEditableWhenFocused.SelectedValueProperty, businessObject, bindingPropertyName, isVisiblePropetyName); if (isEnabledPropertyName != null) { comboBox.SetBinding(ComboBoxEditableWhenFocused.IsReadOnlyProperty, new Binding(isEnabledPropertyName) { Source = businessObject, Converter = new NotBoolConverter() }); } return(comboBox); }
private ComboBoxEditableWhenFocused CreateComboBoxWithNull <T>(object businessObject, IEnumerable <T> items, string displayMemberPath, string bindingPropertyName, string isVisiblePropetyName, string isEnabledPropertyName) where T : class { var style = (Style)FindResource(COMBOBOX_STYLE); var comboBox = new ComboBoxEditableWhenFocused(); var itemType = items.GetType(); var nullValues = NullValue.GetValues("(none)"); comboBox.ItemsSource = new CompositeCollection() { new CollectionContainer() { Collection = nullValues }, new CollectionContainer() { Collection = items } }; var newStyle = new Style(typeof(ComboBoxEditableWhenFocused)); var fef = new FrameworkElementFactory(typeof(TextBlock)); fef.SetBinding(TextBlock.TextProperty, new Binding(displayMemberPath)); var styleResourceDictionary = new ResourceDictionary(); foreach (var trigger in style.Triggers) { newStyle.Triggers.Add(trigger); } foreach (var resourceKey in style.Resources.Keys) { styleResourceDictionary.Add(resourceKey, style.Resources[resourceKey]); } styleResourceDictionary.Add(itemType.GetGenericArguments()[0], new DataTemplate(itemType.GetGenericArguments()[0]) { VisualTree = fef }); newStyle.Resources = styleResourceDictionary; comboBox.Style = newStyle; SetCommunBindings(comboBox, ComboBoxEditableWhenFocused.SelectedValueProperty, new ComboBoxSelectedValueBindingWithNull <T>(businessObject.GetType().GetProperty(bindingPropertyName).GetValue(businessObject, null) ?? nullValues[0], (T value) => businessObject.GetType().GetProperty(bindingPropertyName).SetValue(businessObject, value, null)), "ComboSelectedValue", isVisiblePropetyName); if (isEnabledPropertyName != null) { comboBox.SetBinding(ComboBoxEditableWhenFocused.IsReadOnlyProperty, new Binding(isEnabledPropertyName) { Source = businessObject, Converter = new NotBoolConverter() }); } return(comboBox); }