Skip to content

Commit

Permalink
Fixed XML bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
perdevcoc authored and perdevcoc committed Jul 13, 2015
1 parent 250efdd commit 27fe02c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
63 changes: 46 additions & 17 deletions CoCEd/Common/MyComboBox.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Object> InternalItems = new ObservableCollection<Object>();

static MyComboBox()
{
Expand All @@ -33,33 +37,58 @@ 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;

if (SelectedValue is string)
{
var value = (string)SelectedValue;
var items = ItemsSource.Cast<XmlItem>().ToList();
if (items.Any(x => x.ID == value)) return;
items.Add(new XmlItem { ID = value, Name = value });
ItemsSource = items;
if (InternalItems.Cast<XmlItem>().Any(x => x.ID == value)) return;

InternalItems.Add(new XmlItem { ID = value, Name = value });
}
else
{
var value = (int)SelectedValue;
var items = ItemsSource.Cast<XmlEnum>().ToList();
if (items.Any(x => x.ID == value)) return;
items.Add(new XmlEnum { ID = value, Name = "[ID#: " + value + "] <unknown>" });
ItemsSource = items;
}
}
if (InternalItems.Cast<XmlEnum>().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 + "] <unknown>" });
}
}
}
}
2 changes: 1 addition & 1 deletion CoCEd/Themes/Templates.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type common:MyComboBox}">
<ComboBox x:Name="combo" DisplayMemberPath="Name" SelectedValuePath="ID" Width="150" Margin="0"
ItemsSource="{TemplateBinding ItemsSource}" ItemContainerStyle="{StaticResource MyComboBoxItem}"
ItemContainerStyle="{StaticResource MyComboBoxItem}"
SelectedValue="{Binding Path=SelectedValue, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
Expand Down

0 comments on commit 27fe02c

Please sign in to comment.