From 27fe02ca2faaef8ca5dccf1a3c38255bf24f9ffe Mon Sep 17 00:00:00 2001 From: perdevcoc Date: Mon, 13 Jul 2015 20:45:51 +0000 Subject: [PATCH] Fixed XML bindings. --- CoCEd/Common/MyComboBox.cs | 63 +++++++++++++++++++++++++++---------- CoCEd/Themes/Templates.xaml | 2 +- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/CoCEd/Common/MyComboBox.cs b/CoCEd/Common/MyComboBox.cs index 129ee65..6b97bef 100644 --- a/CoCEd/Common/MyComboBox.cs +++ b/CoCEd/Common/MyComboBox.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,8 +15,10 @@ namespace CoCEd.Common [TemplatePart(Name = "combo", Type = typeof(ComboBox))] public class MyComboBox : Control { - public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(MyComboBox), new PropertyMetadata(null, OnPropertiesChanged)); - public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(Object), typeof(MyComboBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertiesChanged)); + public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(MyComboBox), new PropertyMetadata(null, OnItemsSourceChanged)); + public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(Object), typeof(MyComboBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedValueChanged)); + + readonly ObservableCollection InternalItems = new ObservableCollection(); static MyComboBox() { @@ -33,7 +37,40 @@ public object SelectedValue set { SetValue(SelectedValueProperty, value); } } - void OnPropertiesChanged() + public override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + var combo = Template.FindName("combo", this) as ComboBox; + combo.ItemsSource = InternalItems; + } + + + static void OnItemsSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) + { + MyComboBox box = (MyComboBox)obj; + box.PopulateInternalItems(); + box.AddUnknownItem(); + } + + static void OnSelectedValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) + { + MyComboBox box = (MyComboBox)obj; + box.AddUnknownItem(); + } + + void PopulateInternalItems() + { + if (ItemsSource == null) return; + var selectedValue = SelectedValue; + + InternalItems.Clear(); + foreach (var item in ItemsSource) InternalItems.Add(item); + + SelectedValue = selectedValue; + } + + void AddUnknownItem() { if (ItemsSource == null) return; if (SelectedValue == null) return; @@ -41,25 +78,17 @@ void OnPropertiesChanged() if (SelectedValue is string) { var value = (string)SelectedValue; - var items = ItemsSource.Cast().ToList(); - if (items.Any(x => x.ID == value)) return; - items.Add(new XmlItem { ID = value, Name = value }); - ItemsSource = items; + if (InternalItems.Cast().Any(x => x.ID == value)) return; + + InternalItems.Add(new XmlItem { ID = value, Name = value }); } else { var value = (int)SelectedValue; - var items = ItemsSource.Cast().ToList(); - if (items.Any(x => x.ID == value)) return; - items.Add(new XmlEnum { ID = value, Name = "[ID#: " + value + "] " }); - ItemsSource = items; - } - } + if (InternalItems.Cast().Any(x => x.ID == value)) return; - static void OnPropertiesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) - { - MyComboBox box = (MyComboBox)obj; - box.OnPropertiesChanged(); + InternalItems.Add(new XmlEnum { ID = value, Name = "[ID#: " + value + "] " }); + } } } } diff --git a/CoCEd/Themes/Templates.xaml b/CoCEd/Themes/Templates.xaml index 293cd9e..2383d15 100644 --- a/CoCEd/Themes/Templates.xaml +++ b/CoCEd/Themes/Templates.xaml @@ -232,7 +232,7 @@