为什么我的ItemsControls中的项目没有水平布局?

9

我希望在一个ItemsControl中显示水平排列的项目。

然而,无论我使用Orientation="Horizontal"的StackPanel还是WrapPanel,它们仍然堆叠起来。

如何让ItemsControl中的项目水平排列?

alt text

XAML:

<Window x:Class="TestItemsControl2938.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="400">
    <Window.Resources>
        <DataTemplate x:Key="CustomerListTemplate">
            <StackPanel Width="100" Background="#aaa" Margin="5">
                <TextBlock Text="{Binding LastName}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>       
    <StackPanel>
        <StackPanel Orientation="Horizontal" Background="Orange">
            <ItemsControl ItemsSource="{Binding CustomerList}" ItemTemplate="{StaticResource CustomerListTemplate}"/>
        </StackPanel>
        <WrapPanel Background="Yellow">
            <ItemsControl ItemsSource="{Binding CustomerList}" ItemTemplate="{StaticResource CustomerListTemplate}"/>
        </WrapPanel>
    </StackPanel>
</Window>

代码后台:

using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace TestItemsControl2938
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        private ObservableCollection<Customer> _customerList = new ObservableCollection<Customer>();
        public ObservableCollection<Customer> CustomerList
        {
            get{ return _customerList; }    
            set
            {
                _customerList = value;
                OnPropertyChanged("CustomerList");
            }
        }

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            CustomerList.Add(new Customer { FirstName = "Jim", LastName = "Jones" });
            CustomerList.Add(new Customer { FirstName = "Joe", LastName = "Adams" });
            CustomerList.Add(new Customer { FirstName = "Jake", LastName = "Johnson" });
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }
    }
}

1
你会发送相同的问题多少次? - Arsen Mkrtchyan
这个问题已经被问了3次,可能有些混淆。ID分别是:1228278(这个)、1228270、1228272。1228272现在已经关闭。我建议保持这个开放,并关闭1228270。 - Cheeso
1
抱歉出现了双重发布,目前ajax/图片加载存在一些问题,我想我按了太多次提交按钮。 - Edward Tanguay
2个回答

31

错误的方法。自定义ItemsControl用于包含其项的面板:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
             <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

那我应该把<TextBlock Text="{Binding LastName}"/>放在哪里呢?我正在绑定一个对象,而且我只有一个我的对象类型的水平列表。 - Smithy

1

你应该使用ItemsPanel。看看这里我的回答。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接