WinRT DependencyProperty with IEnumerable not working at all

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
{
    //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
    }

}

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? - , ?

+5
3

, DebugConverter - BindingDebugConverter WinRT XAML Toolkit - , ( Convert ConvertBack, ..).

- , DataContext.

* 1

, , ItemsSource INotifyCollectionChanged true - CollectionChanged, . , ItemsControl , GetContainerForItemOverride IsItemItsOwnContainerOverride.

* 2

, Windows 8 Consumer Preview, IEnumerable . . , :

: "System.Collections.ObjectModel.ObservableCollection`1 [[ACBDP.Person, ACBDP, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null]], System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 ' " IEnumerable "; BindingExpression: Path = 'Person' DataItem = 'ACBDP.BlankPageViewModel, ACBDP, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null'; " ACBDP.AutoCompleteBox "(Name=" null "); target - " ItemsSource "(" IEnumerable").

+2

2

( BaseClass - , , , , , )

1:

typeof(BaseClass) typeof(object). Getter setter BaseClass, InvalidCastException, .

        public BaseClass Item
        {
            get { return (BaseClass)GetValue(ItemProperty); }
            set { SetValue(ItemProperty, value); }
        }
        public static readonly DependencyProperty ItemProperty =
            DependencyProperty.Register("Item", typeof(object), typeof(MyUserControl), new PropertyMetadata(null));

2:

(, , ), . :

#region DummyProperty

public DerivedClass1 Dummy
{
    get { return (DerivedClass1)GetValue(DummyProperty); }
    set { SetValue(DummyProperty, value); }
}

public static readonly DependencyProperty DummyProperty =
    DependencyProperty.Register("Dummy", typeof(DerivedClass1), typeof(MyUserControl), new PropertyMetadata(default(DerivedClass1)));

#endregion

DerivedClass1 BaseClass.

?

, , XAML BaseClass. .

+5

Change typeof (IEnumerable) to typeof (object)

0
source

All Articles