views:

29

answers:

1

I have a Listbox with an itemssource set to an ObservableCollection of DataRow. Let's say each DataRow has 5 columns for this example.

In the DataTemplate of the ListBox I have 5 textblocks (1 for each column). My question is how can I bind to an indexer of the row to get the columns value?

Here is my attempt but nothing displays so I must have the syntax wrong:

<DataTemplate>
    <StackPanel>
        <TextBlock Text="{Binding Path=.[0]}" />
        <TextBlock Text="{Binding Path=.[1]}" />
        <TextBlock Text="{Binding Path=.[2]}" />
        <TextBlock Text="{Binding Path=.[3]}" />
        <TextBlock Text="{Binding Path=.[4]}" />
    </StackPanel>
</DataTemplate>

I know that indexers can be used in bindings because I've done something like this already:

<DataTemplate>
    <StackPanel>
        <TextBlock Text="{Binding Path=Collection[0].Name}" />
        <TextBlock Text="{Binding Path=Collection[1].Name}" />
        <TextBlock Text="{Binding Path=Collection[2].Name}" />
        <TextBlock Text="{Binding Path=Collection[3].Name}" />
        <TextBlock Text="{Binding Path=Collection[4].Name}" />
    </StackPanel>
</DataTemplate>

Any help on correcting my syntax would be appreciated.

A: 

Your syntax should do. However, the following must go also:

<DataTemplate> 
    <StackPanel> 
        <TextBlock Text="{Binding Path=[0]} /> 
        <TextBlock Text="{Binding Path=[1]} /> 
        <TextBlock Text="{Binding Path=[2]} /> 
        <TextBlock Text="{Binding Path=[3]} /> 
        <TextBlock Text="{Binding Path=[4]} /> 
    </StackPanel> 
</DataTemplate> 

Check your ItemsSource. Does it really provide an indexer that is of type int? Maybe you have an indexer of another type or even no index. Maybe it's another object than you expect it to be?

HCL