How to write a custom dependecy property and bind data to a custom type?

I have a project in winrt environment where I am trying to extend the functionality of a standard GridView control by expanding it. The goal is to change the behavior of SelectedItems in a GridView.

public class myGridView : GridView { /// <summary> /// My replacement of SelectedItems /// </summary> public IObservableVector<object> appItems { get { return GetValue(AppSelectedItemsProperty) as IObservableVector<object>; } set { SetValue(AppSelectedItemsProperty, value); appItems.VectorChanged += AppSelectedItemsChanged; } } /// <summary> /// Identifies the AppSelectedItems dependency property. /// </summary> public static readonly DependencyProperty AppSelectedItemsProperty = DependencyProperty.Register( "appItems", typeof(IObservableVector<object>), typeof(AppGridView), new PropertyMetadata(null, AppItemsPropertyCallback)); …. …. ... } 

In my XAML file, I have the following.

 <xx ……. <appControls:AppGridView appItems="{Binding ContactsListSelectedItems, Mode=TwoWay } }"> .… … </appControls:AppGridView > ….. ……. /xx> 

The last part of the code is my MVVM class bound to the Datacontext.

  public class myModel: baseModel { …… …….. /// <summary> /// Not Working /// </summary> private IObservableVector<Contact> _ContactsListSelectedItems; public IObservableVector<Contact> ContactsListSelectedItems { get { return (IObservableVector<Contact>)_ContactsListSelectedItems; } set { SetProperty<IObservableVector<Contact>>(ref _ContactsListSelectedItems, value); } } …. …. } 

I found the implementation of IObservableVector here: https://gist.github.com/runceel/2437074

Binding data with ContactsListSelectedItems does not work, indicating the following error:

 Error: Cannot get 'ContactsListSelectedItems' value (type 'Object') from type 'Consius.ActiveWork.Pages.ContactPage.ContactPageViewModel, Consius.ActiveWork, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='ContactsListSelectedItems' DataItem='Consius.ActiveWork.Pages.ContactPage.ContactPageViewModel, Consius.ActiveWork, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Consius.ActiveWork.Controls.AppGridView' (Name='ContactsList'); target property is 'appItems' (type 'IObservableVector`1<Object>'). 

If I changed the type of ContactsListSelectedItems to:

 IObservableVector<object> 

Everything works well.

This is not an acceptable solution for me by writing my MVVM class using a class object.

Is there anyone there who can give me a clue what is wrong?

+4
source share
2 answers

Have you tried using ObservableCollection instead of IObservableVector and this custom implementation?

+1
source

I am investigating the same problem at the moment. There seem to be two problems. Firstly, if you move your type (contact) to the WinRT component project, it gets rid of the exception that the type cannot be created, because it is not a legitimate WinRT type, but then the second problem arises when the type is not an object, instead virtualization and indexer used enumerator. So far I do not see the way around the problem, except for using the object ...

0
source

Source: https://habr.com/ru/post/1494651/


All Articles