WPF - Dynamically access a specific item of a collection in XAML

Posted by Andy T on Stack Overflow See other posts from Stack Overflow or by Andy T
Published on 2010-06-01T10:18:04Z Indexed on 2010/06/01 10:23 UTC
Read the original article Hit count: 225

Hi,

I have a data source ('SampleAppearanceDefinitions'), which holds a single collection ('Definitions'). Each item in the collection has several properties, including Color, which is what I'm interested in here.

I want, in XAML, to display the Color of a particular item in the collection as text. I can do this just fine using this code below...

Text="{Binding Source={StaticResource SampleAppearanceDefinitions}, Path=Definitions[0].Color}"

The only problem is, this requires me to hard-code the index of the item in the Definitions collection (I've used 0 in the example above). What I want to do in fact is to get that value from a property in my current DataContext ('AppearanceID'). One might imagine the correct code to look like this....

Text="{Binding Source={StaticResource SampleAppearanceDefinitions}, Path=Definitions[{Binding AppearanceID}].Color}"

...but of course, this is wrong.

Can anyone tell me what the correct way to do this is? Is it possible in XAML only? It feels like it ought to be, but I can't work out or find how to do it.

Any help would be greatly appreciated!

Thanks!

AT

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding