views:

251

answers:

1

Hi

I'm trying to create a horizontally oriented stack panel that contains a vertically oriented stack panel of items. Here's my code.

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

Now the code-behind...

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);
            }
        }
    }
}

I'm expecting to see two columns of three blue squares, but instead I see one column of six squares

I can't see anything about the code that jumps out at me as blatantly wrong...

Any ideas?

Thanks

+1  A: 

You've put the root ItemsControl into a StackPanel, when you actually want to put the items in the ItemsControl into the StackPanel. Change to this:

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

HTH,
Kent

Kent Boogaart
Ha! That was exactly it, thanks so much Kent.
Hovito