30

I have a WPF DataGrid bound to ObservableCollection. Each item in my collection has Property which is a List<someObject>. In my row details pane, I would like to write out formatted text blocks for each item in this collection. The end result would be something equivalent to:

<TextBlock Style="{StaticResource NBBOTextBlockStyle}" HorizontalAlignment="Right">
<TextBlock.Inlines>
    <Run FontWeight="Bold" Text="{Binding Path=Exchanges[0].Name}" />
    <Run FontWeight="Bold" Text="{Binding Path=Exchanges[0].Price}" />
    <LineBreak />
    <Run Foreground="LightGray" Text="{Binding Path=Exchanges[0].Quantity}" />
</TextBlock.Inlines>
</TextBlock>
<TextBlock Style="{StaticResource NBBOTextBlockStyle}">
<TextBlock.Inlines>
    <Run FontWeight="Bold" Text="{Binding Path=Exchanges[1].Name}" />
    <Run FontWeight="Bold" Text="{Binding Path=Exchanges[1].Price}" />
    <LineBreak />
    <Run Foreground="LightGray" Text="{Binding Path=Exchanges[1].Quantity}" />
</TextBlock.Inlines>
</TextBlock>

and so on 0-n times.

I've tried using ItemsControl for this:

<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <DataTemplate>
        <Label>test</Label>
    </DataTemplate>
</ItemsControl>

however, this appears to be only meant for more static sources, as it throws the following exception (collection is not altered after creation):

ItemsControl Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead*

Is there another way to achieve this?

1
  • The ItemsControl should be fine. You usually get this error when using the Items property of the ItemsControl while your ItemsSource is bound, could that be the case? Jun 9, 2010 at 21:39

1 Answer 1

69

What you did by specifying <DataTemplate .../> inside of ItemsControl is you added this instance of DataTemplate to default property of ItemsControl which is Items. So the exception you got is the expected result: first you specify the ItemsSource, then you modify Items. Instead you should modify ItemTemplate property on your ItemsControl like so:

<ItemsControl ItemsSource="{Binding Path=Exchanges}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Label>test</Label>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
3
  • awesome, thanks. anyway i can make the items in that template stack up horizontally instead of vertically?
    – Sonic Soul
    Jun 10, 2010 at 0:26
  • 1
    Change the ItemsPanel to use an ItemsPanelTemplate with a StackPanel that has Orientation="Horizontal".
    – John Bowen
    Jun 10, 2010 at 1:01
  • 1
    Edit: added sample ItemPanelTemplate suggested by John. You can use WrapPanel instead of StackPanel, if you want line wrapping.
    – repka
    Jun 10, 2010 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.