ListBox中ListBoxItem默认是纵向排列,可以通过自定义样式,让其横向排列,
如下Demo:
XAML:
C#:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Collections.ObjectModel;using System.ComponentModel;namespace ListBoxItemStyleDemo{ ////// MainWindow.xaml 的交互逻辑(横着放ListBoxItem) /// public partial class MainWindow : Window,INotifyPropertyChanged { private string terminalName; ////// 机台(站点) /// public string TerminalName { get { return terminalName; } set { terminalName = value; OnPropertyChanged("TerminalName"); } } private DateTime productionDay; ////// 工作日 /// public DateTime ProductionDay { get { return productionDay; } set { productionDay = value; OnPropertyChanged("ProductionDay"); } } private string shiftName; ////// 班次 /// public string ShiftName { get { return shiftName; } set { shiftName = value; } } private ObservableCollectioncars; /// /// 汽车集合 /// public ObservableCollectionCars { get { if (cars==null) { cars = new ObservableCollection (); } return cars; } } private Car selectedCar; /// /// 选中的汽车 /// public Car SelectedCar { get { return selectedCar; } set { selectedCar = value; OnPropertyChanged("SelectedCar"); } } public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { this.TerminalName = "24#站点"; this.ProductionDay = DateTime.Now; this.ShiftName = "早班"; this.Cars.Add(new Car { CarNo = "0001#", CarName = "秋刀鱼牌汽车", WorkOrder = "20160402A", Customer = "TYDF86" }); this.Cars.Add(new Car { CarNo = "0002#", CarName = "武昌鱼牌汽车", WorkOrder = "20160402A", Customer = "SEDF75" }); this.Cars.Add(new Car { CarNo = "0003#", CarName = "金枪鱼牌汽车", WorkOrder = "20160403A", Customer = "CKDF34" }); this.Cars.Add(new Car { CarNo = "0004#", CarName = "沙丁鱼牌汽车", WorkOrder = "20160404A", Customer = "RSDF21" }); this.Cars.Add(new Car { CarNo = "0005#", CarName = "三文鱼牌汽车", WorkOrder = "20160405A", Customer = "YRDF56" }); } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (this.PropertyChanged!=null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } public class Car:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } private string workOrder; ////// 工单号 /// public string WorkOrder { get { return workOrder; } set { workOrder = value; OnPropertyChanged("WorkOrder"); } } private string carNo; ////// CarNo /// public string CarNo { get { return carNo; } set { carNo = value; OnPropertyChanged("CarNo"); } } private string carName; ////// 品牌 /// public string CarName { get { return carName; } set { carName = value; OnPropertyChanged("CarName"); } } private string customer; ////// 客户代码 /// public string Customer { get { return customer; } set { customer = value; OnPropertyChanged("Customer"); } } }}
运行效果:
选择“0003#”:
总结:
设置ListBox的属性如下:
则ListBox的ListBoxItem横向排列。