【发布时间】:2010-03-05 00:56:30
【问题描述】:
我正在尝试使用 Silverlight 3 在 ItemsControl DataTemplate 中设置 Canvas 属性。根据this post,这样做的唯一方法是使用 ContentPresenter 类型的 ItemsContainerStyle 设置它,因为 Canvas 属性仅生效在画布的直接子代上。这似乎在 SL3 中不起作用,因为 ItemsControl 没有 ItemsContainerStyle 属性,所以我按照this article 的建议尝试了一个 ListBox,但它仍然不起作用。从下面的 XAML 中,我希望看到一个绿色正方形,数字 10、30、50、70 从“NW”到“SE”方向级联。谁能告诉我为什么他们都在西北角叠在一起?
<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>
【问题讨论】:
-
感谢 skb,这回答了我的问题,即如何在 WPF 中执行此操作 :-) 正如您所说,它在 WPF 中效果很好。
标签: silverlight silverlight-3.0 listbox canvas