加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么我的ItemsControls中的项目不会水平布局?

发布时间:2020-12-15 17:59:59 所属栏目:百科 来源:网络整理
导读:我想要一个ItemsControl,其中项目是水平显示的. 然而,无论我使用StackPanel与Orientation =“Horizo??ntal”还是WrapPanel,它们仍然堆叠起来. 如何将ItemsControl中的项目水平放置? alt text http://i31.tinypic.com/dd2et5.png XAML: Window x:Class="Tes
我想要一个ItemsControl,其中项目是水平显示的.

然而,无论我使用StackPanel与Orientation =“Horizo??ntal”还是WrapPanel,它们仍然堆叠起来.

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

alt text http://i31.tinypic.com/dd2et5.png

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

解决方法

错误的方式自定义ItemsControl用于包含其项目的面板:
<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
             <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读