I am combing and washing, as I know that there were a lot of messages about Dependency properties, but I just did not see anything where there is a solution that works. I am trying to bind an ObservableCollection from my ViewModel to AutoCompleteBox autoload. My ViewModel returns data, and Getter gets. However, after that, the SetValue or OnItemsSourcePropertyChanged control fails. Any thoughts on what might be wrong?
I have a control like this:
[ContentProperty(Name = "ItemsSource")]
public partial class AutoCompleteBox : Control
{
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); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource",
typeof(IEnumerable),
typeof(AutoCompleteBox),
new PropertyMetadata(null, OnItemsSourcePropertyChanged));
private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
#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)
{
}
}
The following is an example of using a control:
<toolkit:AutoCompleteBox Grid.Row="5" Grid.Column="2" ItemsSource="{Binding Persons,Mode=TwoWay}" SearchText="WhatTheHell"/>
As a test and for simplicity, I created a String DependencyProperty for SearchText. It works fine if I bind SearchText, OnSearchTextPropertyChanged is called:
<toolkit:AutoCompleteBox Grid.Row="5" Grid.Column="2" ItemsSource="{Binding Persons,Mode=TwoWay}" SearchText="{Binding SearchText}"/>
- WinRT? - , ?