views:

324

answers:

1

In WPF, you can create a ListBox with a Canvas as an ItemsPanel and position items on that canvas. The code to do that looks something like this:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Width="200" Height="200"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Canvas.Left" Value="{Binding Path=XPos}"/>
            <Setter Property="Canvas.Top" Value="{Binding Path=YPos}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Can you do the same thing in a Silverlight2 ListBox, or preferably an ItemsControl?

+2  A: 

I've found a solution, but (to me) it smells.

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Canvas Width="200" Height="200">
                <TextBlock 
                    Text="{Binding Path=Name}" 
                    Canvas.Left="{Binding Path=XPos}" 
                    Canvas.Top="{Binding Path=YPos}" />
            </Canvas>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Width="200" Height="200"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

Each item has its own canvas, so they end up stacked on top of eachother.

geofftnz