在 WPF 中,您可以使用 Canvas 创建一个 ListBox 作为 ItemsPanel 并在该 Canvas 上定位项目。执行此操作的代码如下所示:
<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 中做同样的事情?
最佳答案
我找到了解决方案,但(对我来说)它有味道。
<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>
每个项目都有自己的 Canvas ,因此它们最终会堆叠在一起。
关于silverlight - 如何使用 Silverlight2 ItemsControl 在 Canvas 上定位项目集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/822680/