在 WPF 中,您可以创建一个带有 Canvas 作为 ItemsPanel 的 ListBox,并在该画布上放置项目。执行此操作的代码如下所示:
<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>
你能在 Silverlight2 ListBox 或者最好是 ItemsControl 中做同样的事情吗?