【问题标题】:Why don't the items in my ItemsControls layout horizontally?为什么我的 ItemsControls 中的项目不水平布局?
【发布时间】:2009-08-04 15:43:15
【问题描述】:

我想要一个 ItemsControl,其中项目水平显示

但是,无论我将 StackPanel 与 Orientation="Horizo​​ntal" 还是 WrapPanel 一起使用,它们仍然会堆叠起来。

如何使 ItemsControl 中的项目水平放置?

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

【问题讨论】:

  • 同一个问题你会发送多少次?
  • 这个问题已经被问了 3 次了。我敢肯定,有些混淆。 id 是:1228278(这个)、1228270、1228272。1228272 现已关闭。我建议保持这个打开,关闭 1228270。
  • 对不起,双重发布,目前加载 ajax / 图像有一些问题,我想我按提交太多次了

标签: c# wpf xaml itemscontrol


【解决方案1】:

走错路了。自定义 ItemsControl 用来包含其项目的面板:

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

【讨论】:

  • 我会把&lt;TextBlock Text="{Binding LastName}"/&gt;放在哪里??我绑定到一个对象,我只有一个对象类型的水平列表。
【解决方案2】:

你应该使用项目面板。看here我的回答

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多