2009-12-05 34 views
0

我試圖創建一個包含項目的垂直面向堆棧面板水平方向的堆疊面板。這是我的代碼。奇怪ItemsPanelTemplate問題(可能Silverlight的錯誤?)

首先XAML

<UserControl x:Class="SilverlightApplication1.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
> 

<UserControl.Resources> 
    <DataTemplate x:Key="SquareTemplate"> 
     <Border Margin="2" Background="Blue" Width="80" Height="80"/> 
    </DataTemplate> 

    <DataTemplate x:Key="VerticallyTiledItemTemplate"> 
     <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource SquareTemplate}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Vertical" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </DataTemplate>   
</UserControl.Resources> 

<Grid x:Name="LayoutRoot"> 
    <StackPanel Orientation="Horizontal"> 
     <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource VerticallyTiledItemTemplate}"/> 
    </StackPanel> 
</Grid> 
</UserControl> 

現在的代碼隱藏...

namespace SilverlightApplication1 
{ 
    public partial class MainPage : UserControl 
    { 
     public ObservableCollection<ObservableCollection<object>> _myCollection = new ObservableCollection<ObservableCollection<object>>(); 

     public ObservableCollection<ObservableCollection<object>> MyCollection 
     { 
      get 
      { 
       return _myCollection; 
      } 
      set 
      { 
       _myCollection = value; 
      } 
     } 

     public MainPage() 
     { 
      InitializeComponent(); 
      LayoutRoot.DataContext = MyCollection; 

      for (int i = 0; i < 2; i++) 
      { 
       var innerCollection = new ObservableCollection<object>(); 

       for (int j = 0; j < 3; j++) 
       { 
        innerCollection.Add(new object()); 
       } 

       _myCollection.Add(innerCollection); 
      } 
     } 
    } 
} 

我期待看到三個藍色方形兩列,而是我看到一列六格

我可以'沒有看到任何關於跳出我的代碼公然錯誤...

任何想法?

感謝

回答

1

你已經把根ItemsControlStackPanel,當你真正要放在ItemsControl的物品進入StackPanel。更改爲:

<Grid x:Name="LayoutRoot"> 
    <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource VerticallyTiledItemTemplate}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</Grid> 
+1

哈!就是這樣,非常感謝肯特。 – Senkwe 2009-12-05 13:50:42