我正在尝试在Silverlight 3中使用ItemsControl DataTemplate设置Canvas属性。根据此帖子,唯一的方法是使用ContentPresenter类型的ItemsContainerStyle进行设置,因为Canvas属性只对Canvas的直接子元素生效。但是这在SL3中似乎不起作用,因为ItemsControl没有ItemsContainerStyle属性,所以我尝试了ListBox,正如这篇文章所建议的,但仍然不起作用。从下面的XAML中,我希望看到一个绿色的正方形,并且数字10、30、50、70从“NW”到“SE”方向递增。有人能告诉我为什么它们都堆叠在NW角上吗?
<UserControl x:Class="TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib" >
<StackPanel>
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Green" Width="100" Height="100" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding}" />
<Setter Property="Canvas.Top" Value="{Binding}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Items>
<System:Int32>10</System:Int32>
<System:Int32>30</System:Int32>
<System:Int32>50</System:Int32>
<System:Int32>70</System:Int32>
</ListBox.Items>
</ListBox>
</StackPanel>
</UserControl>