首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的ItemsPanelTemplate问题(可能是Silverlight bug?)

奇怪的ItemsPanelTemplate问题(可能是Silverlight bug?)
EN

Stack Overflow用户
提问于 2009-12-05 12:54:59
回答 1查看 774关注 0票数 0

我正在尝试创建一个面向水平的堆栈面板,该面板包含一个垂直方向的项堆栈面板。这是我的密码。

首先是XAML

代码语言:javascript
复制
<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>

现在密码隐藏..。

代码语言:javascript
复制
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);
            }
        }
    }
}

我希望看到两列三蓝色方格,但我看到一列六方格

我看不出我的代码有什么明显的错误.

有什么想法吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-12-05 13:24:00

您已经将根ItemsControl放入StackPanel中,而实际上您希望将ItemsControl中的项放入StackPanel中。改为:

代码语言:javascript
复制
<Grid x:Name="LayoutRoot">
    <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource VerticallyTiledItemTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1852140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档