Adam Rackis
Adam Rackis

Reputation: 83376

Silverlight 4 - simple control template

    <DataTemplate x:Key="dirtSimple">
        <TextBlock Margin="10,0,0,0" Text="{Binding Path=CurrentBook.Published, StringFormat=d}"></TextBlock>
    </DataTemplate>

    <ControlTemplate x:Key="lbWrapPanelTemplate">
        <StackPanel Orientation="Horizontal" Margin="2" Background="Aqua">
            <ItemsPresenter></ItemsPresenter>
        </StackPanel>
    </ControlTemplate>

...

    <ListBox Template="{StaticResource lbWrapPanelTemplate}" x:Name="bookListBox"  Grid.Row="0" ItemsSource="{Binding Path=BookSource}" ItemTemplate="{StaticResource dirtSimple}"  >
    </ListBox>

The list box is displaying correctly, with a beautiful "Aqua" background, and each item is boringly displayed with just a date. For some reason though the items are not flowing horizontally. I originally tried it with the Silverlight Toolkit's WrapPanel, with the same problem, but I can't even get it to work with a built-in StackPanel, so I suspect I'm missing something.

Upvotes: 0

Views: 755

Answers (1)

Adam Sills
Adam Sills

Reputation: 17062

Are you trying to get selection-based behavior that a ListBox provides? If not, use an ItemsControl (and supply an ItemsPanel as below).

The reason it's not going horizontal is the ItemsPresenter ultimately has its own panel it lays out items in. It's not inserting each item separately into your StackPanel (or WrapPanel), it's putting them in its own panel

What you want to do is specify a value for ItemsPanel like so:

<ListBox ItemTemplate="{StaticResource dirtSimple}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

Upvotes: 1

Related Questions