Binding to Indexed Item (Dictionary, array, and custom classes) #110

Closed
ad1Dima opened this Issue Jan 16, 2013 · 10 comments

Comments

Projects
None yet
2 participants
@ad1Dima

ad1Dima commented Jan 16, 2013

In WP7 you can bind like this:

Text = {Binding Object.Dictionary[Key].Property}

But MVX can't bind to item in dictionary like this.

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 16, 2013

Contributor

Interesting - that's not a binding I've ever seen or used :)

Do you know if that element binding also works with INotifyCollectionChanged? And with IObservableDictionary in WinRt Or is it just fixed?


Is it possible you can also post more details on what types are allowed in Key? e.g. some examples or a link?


I'll link this to #87 - as the collection thing has been discussed there...


As a workaround in the short term, then you can probably achieve the same effect using a ValueConverter

e.g. Text = {Binding Path=CollectionObject, Converter=ExtractMagic, ConverterParameter=Key}

where ExtractMagic Convert() casts the CollectionObject to a dictionary and then uses the parameter to extract what you need.

However, I don't think that converter route would watch for INotifyCollectionChanged notifications currently.

Contributor

slodge commented Jan 16, 2013

Interesting - that's not a binding I've ever seen or used :)

Do you know if that element binding also works with INotifyCollectionChanged? And with IObservableDictionary in WinRt Or is it just fixed?


Is it possible you can also post more details on what types are allowed in Key? e.g. some examples or a link?


I'll link this to #87 - as the collection thing has been discussed there...


As a workaround in the short term, then you can probably achieve the same effect using a ValueConverter

e.g. Text = {Binding Path=CollectionObject, Converter=ExtractMagic, ConverterParameter=Key}

where ExtractMagic Convert() casts the CollectionObject to a dictionary and then uses the parameter to extract what you need.

However, I don't think that converter route would watch for INotifyCollectionChanged notifications currently.

@ad1Dima

This comment has been minimized.

Show comment
Hide comment
@ad1Dima

ad1Dima Jan 16, 2013

  1. i don' know
  2. here is link. I think it will help

ad1Dima commented Jan 16, 2013

  1. i don' know
  2. here is link. I think it will help
@ad1Dima

This comment has been minimized.

Show comment
Hide comment
@ad1Dima

ad1Dima Jan 16, 2013

Here is link that really helps http://msdn.microsoft.com/ru-ru/library/cc645024(v=vs.95).aspx

Indexers can be used for accessing properties in a path and obtaining items from a list, but with some notable restrictions:

  • Numeric integer indexers are supported.
  • Beginning in Silverlight 4, string indexers are supported.
  • Only one-dimensional array indexing is supported.
  • The type being indexed must implement or inherit IList. (List<(Of <(<'T>)>)> is accepted, because it implements IList. However IList<(Of <(<'T>)>)> is not accepted.)

ad1Dima commented Jan 16, 2013

Here is link that really helps http://msdn.microsoft.com/ru-ru/library/cc645024(v=vs.95).aspx

Indexers can be used for accessing properties in a path and obtaining items from a list, but with some notable restrictions:

  • Numeric integer indexers are supported.
  • Beginning in Silverlight 4, string indexers are supported.
  • Only one-dimensional array indexing is supported.
  • The type being indexed must implement or inherit IList. (List<(Of <(<'T>)>)> is accepted, because it implements IList. However IList<(Of <(<'T>)>)> is not accepted.)
@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 16, 2013

Contributor

Just ran a quick test on WP7 and it does seem to be listening to INotifyCollectionChanged notifications - which does make the problem harder....

Contributor

slodge commented Jan 16, 2013

Just ran a quick test on WP7 and it does seem to be listening to INotifyCollectionChanged notifications - which does make the problem harder....

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 16, 2013

Contributor

Note to self: if you implement INotifyCollectionChanged binding then you may end up introducing memory leaks...

Contributor

slodge commented Jan 16, 2013

Note to self: if you implement INotifyCollectionChanged binding then you may end up introducing memory leaks...

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 17, 2013

Contributor

It would seem that I was perhaps wrong about INotifyCollectionChanged binding...

It seems like the Mono ObserverableCollection fires:

OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));

from https://github.com/mosa/Mono-Class-Libraries/blob/master/mcs/class/System/System.Collections.ObjectModel/ObservableCollection.cs#L136

Interesting :)

Contributor

slodge commented Jan 17, 2013

It would seem that I was perhaps wrong about INotifyCollectionChanged binding...

It seems like the Mono ObserverableCollection fires:

OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));

from https://github.com/mosa/Mono-Class-Libraries/blob/master/mcs/class/System/System.Collections.ObjectModel/ObservableCollection.cs#L136

Interesting :)

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 18, 2013

Contributor

As first step.... can you code review this Gist?

https://gist.github.com/4561366

Will that class tokenise strings like:

  • P1
  • P1.P2
  • P1[1]
  • P1["key"]
  • P1[1].P2
  • P1[1].P2['key']
  • P1["key"with\rcharacters"]
  • etc...

Got to get that tokenising working first, then the actual binding will be quite quick to add I think :)

G night!

Stuart

Contributor

slodge commented Jan 18, 2013

As first step.... can you code review this Gist?

https://gist.github.com/4561366

Will that class tokenise strings like:

  • P1
  • P1.P2
  • P1[1]
  • P1["key"]
  • P1[1].P2
  • P1[1].P2['key']
  • P1["key"with\rcharacters"]
  • etc...

Got to get that tokenising working first, then the actual binding will be quite quick to add I think :)

G night!

Stuart

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 18, 2013

Contributor

First version of this checked in to the vNextDialog branch - https://github.com/slodge/MvvmCross/tree/vNextDialog

Tested on Twitter on Droid using an image view:

<Mvx.MvxHttpImageView
android:id="@+id/TwitterImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="4dp"
android:src="@drawable/icon"
local:MvxBind="{'HttpImageUrl':{'Path':'Tweets[0].ProfileImageUrl'}}"
/>

Not been tested on MonoTouch yet

Names and namespaces might be changed.... It also desperately needs a test harness...

Contributor

slodge commented Jan 18, 2013

First version of this checked in to the vNextDialog branch - https://github.com/slodge/MvvmCross/tree/vNextDialog

Tested on Twitter on Droid using an image view:

<Mvx.MvxHttpImageView
android:id="@+id/TwitterImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="4dp"
android:src="@drawable/icon"
local:MvxBind="{'HttpImageUrl':{'Path':'Tweets[0].ProfileImageUrl'}}"
/>

Not been tested on MonoTouch yet

Names and namespaces might be changed.... It also desperately needs a test harness...

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 18, 2013

Contributor

Here's the commit 16bc44c

Contributor

slodge commented Jan 18, 2013

Here's the commit 16bc44c

@slodge

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Jan 21, 2013

Contributor

I'm closing this - it's checked in on vnext... :)

Thanks again for the info - especially for digging out the MSDN refs

Contributor

slodge commented Jan 21, 2013

I'm closing this - it's checked in on vnext... :)

Thanks again for the info - especially for digging out the MSDN refs

@slodge slodge closed this Jan 21, 2013

martijn00 added a commit to martijn00/MvvmCross that referenced this issue Dec 8, 2016

Merge pull request #123 from thefex/master
Build fix of Preferences/Leanback projects. Fix non-generic fragment restore crash (#110)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment