I've scoured and scoured since I know there has been a lot posted about Dependency Properties but I just haven't quite seen anything out there that has a solution that works. I'm trying to bind an ObservableCollection from my ViewModel to my AutoCompleteBox. My ViewModel is returning the data, the Getter is being hit. However, after that, the control's SetValue or OnItemsSourcePropertyChanged doesn't fire. Any thoughts as to what might be wrong?
I've got a control like so:
[ContentProperty(Name = "ItemsSource")]
public partial class AutoCompleteBox : Control
{
//local stuff
private ListBox lb;
private List<Person> _items;
private ObservableCollection<Person> _view;
public AutoCompleteBox() : base()
{
DefaultStyleKey = typeof(AutoCompleteBox);
Loaded += (sender, e) => ApplyTemplate();
}
protected override void OnApplyTemplate()
{
this.lb = this.GetTemplateChild("Selector") as ListBox;
base.OnApplyTemplate();
}
#region ItemsSource
public IEnumerable ItemsSource
{
get { return GetValue(ItemsSourceProperty) as ObservableCollection<Person>; }
set { SetValue(ItemsSourceProperty, value); } //Never gets called
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource",
typeof(IEnumerable),
typeof(AutoCompleteBox),
new PropertyMetadata(null, OnItemsSourcePropertyChanged));
private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Never gets called :(
}
#endregion
public String SearchText
{
get { return GetValue(SearchTextProperty) as String; }
set
{
SetValue(SearchTextProperty, value);
}
}
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register(
"SearchText",
typeof(String),
typeof(AutoCompleteBox),
new PropertyMetadata(null, OnSearchTextPropertyChanged));
private static void OnSearchTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//gets fired when loaded, with data being bound
}
}
Below is the how it is being used the control:
<toolkit:AutoCompleteBox Grid.Row="5" Grid.Column="2" ItemsSource="{Binding Persons,Mode=TwoWay}" SearchText="WhatTheHell"/>
As a test and for simplicity sake, I created a String DependencyProperty for SearchText. It works fine if I bind the SearchText, the OnSearchTextPropertyChanged is called:
<toolkit:AutoCompleteBox Grid.Row="5" Grid.Column="2" ItemsSource="{Binding Persons,Mode=TwoWay}" SearchText="{Binding SearchText}"/>
Anybody else experience these issues with WinRT? Or see anything wrong with what I am doing?