2009-05-04 57 views
3

在WPF中,您可以使用Canvas創建一個ListBox作爲ItemsPanel並將項目放置在該畫布上。做到這一點的代碼看起來是這樣的:在Silverlight2列表框如何使用Silverlight2 ItemsControl在畫布上定位項目集合?

<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> 

你可以做同樣的事情,最好一個ItemsControl?

回答

2

我發現a解決方案,但(對我來說)它聞起來。

<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> 

每個項目都有自己的畫布,所以他們最終堆疊在彼此的頂部。

相關問題