How to use a Silverlight2 ItemsControl element to place a collection of elements on a canvas?

In WPF, you can create a ListBox with a Canvas as an ItemsPanel and position the positions on this canvas. The code for this 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 in the Silverlight2 ListBox, or preferably in the ItemsControl?

+3
source share
1 answer

I found solution a , but (for 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 element has its own canvas, so it ends on top of eachother.

+2
source

Source: https://habr.com/ru/post/1707639/


All Articles