0

アイテムの垂直方向のスタック パネルを含む水平方向のスタック パネルを作成しようとしています。これが私のコードです。

最初に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);
            }
        }
    }
}

3 つの青い正方形の 2 つの列が表示されることを期待していますが、代わりに6 つの正方形の 1 つの列が表示されます

露骨に間違っていると飛び出すコードについては何もわかりません...

何か案は?

ありがとう

4

1 に答える 1

1

実際にアイテムを に入れたいときに、ルートItemsControlをに入れました。これに変更します。StackPanelItemsControlStackPanel

<Grid x:Name="LayoutRoot">
    <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource VerticallyTiledItemTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>
于 2009-12-05T13:24:00.253 に答える