Is a variable binding to a collection item possible

binding, c#, collections, variables, wpf

Solution

Yes, it is possible. You should implement binding converter that will convert collection to collection item and take index as converter parameter. Then you'll use it like this:

<TextBlock Text="{Binding Fields, 
                  Converter={StaticResource CollectionToItemConverter,
                  ConverterParameter={Binding Pos}}}" />

If you need a code for this converter or additional info about converters, please leave a comment.

Hope it helps.

Problem

I'm trying to bind to an item inside a collection but the index for that item needs to be "variable". Take the following pseudo syntax for example: ``` <TextBlock Text="{Binding Fields[{Binding Pos}]}" /> ``` Is something like this possible? If my property Pos is 1 it should bind to the first item out of the collection "Fields" and if my Pos is 3 it should bind to the third item in the collection. I simplified my problem to this situation... Is something like this doable and how?

Original source