ListBox中ListBoxItem默认是纵向排列,可以通过自定义样式,让其横向排列,

如下Demo:

XAML:

<Window x:Class="ListBoxItemStyleDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}" Loaded="Window_Loaded">
<Window.Background>
<ImageBrush ImageSource="/ListBoxItemStyleDemo;component/Resources/background.jpg" />
</Window.Background>
<Window.Resources>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ListBoxItemStyle1">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Width" Value="150" />
<Setter Property="FontSize" Value="20" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="border" CornerRadius="8" Margin="2">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="border" Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF6B6B6B" Offset="0.5"/>
<GradientStop Color="#FF535353" Offset="0.5"/>
<GradientStop Color="#FF535353" Offset="0.022"/>
<GradientStop Color="#FF555555" Offset="0.991"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DockPanel>
<Border DockPanel.Dock="Top" BorderBrush="White" BorderThickness="0,3" Height="50" Margin="0,8" Padding="10,0">
<Border.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF6EADD4" Offset="0.013"/>
<GradientStop Color="#FF6FB6E4" Offset="0.5"/>
<GradientStop Color="#FF64A6D0" Offset="0.53"/>
<GradientStop Color="#FF5E97BA" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30*" />
<RowDefinition Height="14*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ListBox x:Name="lstEnt" Margin="0,-8" BorderBrush="{x:Null}" Background="{x:Null}" ItemContainerStyle="{StaticResource ResourceKey=ListBoxItemStyle1}"
ItemsSource="{Binding Path=Terminal.Entities}" SelectedItem="{Binding Path=SelectedEntity}" DisplayMemberPath="Name" DataContext="{Binding}" Grid.RowSpan="2">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" TextElement.Foreground="White" TextElement.FontSize="24" Margin="0,5,0,7" Grid.RowSpan="2">
<TextBlock x:Name="txtTitle" Text="秋刀鱼汽车厂"/>
<TextBlock Text="{Binding Path=TerminalName}" />
</StackPanel>
</Grid>
</Border>
<Grid DockPanel.Dock="Top" Margin="10,2" TextElement.FontSize="16">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Text="{Binding Path=ProductionDay, StringFormat=yyyy-MM-dd}"/>
<TextBlock x:Name="shiftTxt" Text="{Binding Path=ShiftName}" Margin="10,0,0,0" />
</StackPanel>
</Grid>
<Grid DockPanel.Dock="Top">
<ListBox x:Name="ProcessTankListBox" BorderBrush="{x:Null}" Background="#FF518CB1" ItemContainerStyle="{StaticResource ResourceKey=ListBoxItemStyle1}"
ItemsSource="{Binding Path=Cars}" SelectedItem="{Binding Path=SelectedCar}" DisplayMemberPath="CarNo" DataContext="{Binding}" Grid.RowSpan="2" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
<Border x:Name="border" DockPanel.Dock="Top" Padding="10,5" Margin="5" BorderThickness="1,30,1,3" CornerRadius="10" BorderBrush="#FF518CB1"
DataContext="{Binding Path=SelectedTank}">
<Border.Background>
<SolidColorBrush Color="#99FFFFFF" Opacity="100" />
</Border.Background>
<Grid DataContext="{Binding Path=DataContext.SelectedCar, RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}}" TextElement.FontSize="16" TextElement.Foreground="#FF063958">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90" />
<ColumnDefinition />
<ColumnDefinition Width="8" />
<ColumnDefinition Width="80" />
<ColumnDefinition />
<ColumnDefinition Width="8" />
<ColumnDefinition Width="90" />
<ColumnDefinition />
<ColumnDefinition Width="8" />
<ColumnDefinition Width="80" />
<ColumnDefinition />
<!--<ColumnDefinition />-->
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="4" />
<RowDefinition />
<RowDefinition Height="2" />
<RowDefinition />
<RowDefinition Height="20" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="11" FontSize="18" Text="汽车基础信息" Margin="0,-32,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="White" >
</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Text="工单号:" />
<TextBlock Grid.Row="2" Grid.Column="3" Text="品牌:" />
<TextBlock Grid.Row="2" Grid.Column="6" Text="流水号:" />
<TextBlock Grid.Row="2" Grid.Column="9" Text="客户代码:" /> <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=WorkOrder}" />
<TextBlock Grid.Row="2" Grid.Column="4" Text="{Binding Path=CarName}" />
<TextBlock Grid.Row="2" Grid.Column="7" Text="{Binding Path=CarNo}" />
<TextBlock Grid.Row="2" Grid.Column="10" Text="{Binding Path=Customer}" />
</Grid>
</Border>
</DockPanel>
</Grid>
</Window>

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
{
/// <summary>
/// MainWindow.xaml 的交互逻辑(横着放ListBoxItem)
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
private string terminalName;
/// <summary>
/// 机台(站点)
/// </summary>
public string TerminalName
{
get { return terminalName; }
set
{
terminalName = value;
OnPropertyChanged("TerminalName");
}
} private DateTime productionDay;
/// <summary>
/// 工作日
/// </summary>
public DateTime ProductionDay
{
get { return productionDay; }
set
{
productionDay = value;
OnPropertyChanged("ProductionDay");
}
} private string shiftName;
/// <summary>
/// 班次
/// </summary>
public string ShiftName
{
get { return shiftName; }
set { shiftName = value; }
} private ObservableCollection<Car> cars;
/// <summary>
/// 汽车集合
/// </summary>
public ObservableCollection<Car> Cars
{
get
{
if (cars==null)
{
cars = new ObservableCollection<Car>();
}
return cars;
}
} private Car selectedCar;
/// <summary>
/// 选中的汽车
/// </summary>
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;
/// <summary>
/// 工单号
/// </summary>
public string WorkOrder
{
get { return workOrder; }
set
{
workOrder = value;
OnPropertyChanged("WorkOrder");
}
} private string carNo;
/// <summary>
/// CarNo
/// </summary>
public string CarNo
{
get { return carNo; }
set
{
carNo = value;
OnPropertyChanged("CarNo");
}
} private string carName;
/// <summary>
/// 品牌
/// </summary>
public string CarName
{
get { return carName; }
set
{
carName = value;
OnPropertyChanged("CarName");
}
} private string customer;
/// <summary>
/// 客户代码
/// </summary>
public string Customer
{
get { return customer; }
set
{
customer = value;
OnPropertyChanged("Customer");
}
} }
}

运行效果:

选择“0003#”:

总结:

设置ListBox的属性如下:

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

则ListBox的ListBoxItem横向排列。

wpf之ListBox中ListBoxItem横向排列的更多相关文章

  1. WPF listbox中Checkbox横向排列

    <ListBox Height="220" Margin="0" ItemsSource="{Binding RightCollection}& ...

  2. WPF的ListBox中的RadioButton不能单选问题

    WPF不知道是微软故意弄的还是真的匆忙的推出的产品,在实际开发过程中有很多小问题解决很麻烦. 今天主要说一下ListBox中使用RadioButton的时候里面的RadioButton不能单选!居然成 ...

  3. WPF 自定义ListBox

     如题,要实现一个如下的列表,该如何实现? 在设计过程中,会遇到如下问题: 1.ListBox中ListBoxItem的模板设计 2.ListBox中ListBoxItem的模板容器设计 3.List ...

  4. WPF ListBox 横向排列

    WPF ListBox 横向排列   如果只是单纯的让ListBox可以横向配列,这样很简单,只需要更改ListBox的ItemsPanel模板就可以,例如: <ListBox><L ...

  5. WPF关于改变ListBoxItem的颜色的注意事项以及如何找到ListBox中的ItemsPanel

    在ListBox中碰到过几个问题,现在把它写出来: 第一个就是在ListBoxItem中当我用触发器IsSelected和IsMouseOver来设置Background和Foreground的时候, ...

  6. Template、ItemsPanel、ItemContainerStyle、ItemTemplate(包括ListBox的Item子项是横向排列)

    Template.ItemsPanel.ItemContainerStyle.ItemTemplate 分类: WPF2011-10-12 10:13 4716人阅读 评论(0) 收藏 举报 data ...

  7. WPF绑定的ListBox获取ListBoxItem及GoToState应用

    现公司项目中需要制作一个扇形菜单,菜单项是用ListBox重写Style实现的,其数据是绑定的.菜单的每一项都有Normal,MouseOver和Selected三种状态,这三种状态当然可以通过鼠标移 ...

  8. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

  9. listbox横向排列

    在Listbox中横向显示CheckBox 前台代码 <ListBox Height=" > <StackPanel x:Name="sp" Orien ...

随机推荐

  1. MySQL data路径为空解决方法

    CMD x:\mysql\5.7.12\bin>mysqld --initialize-insecure --user=mysql

  2. sealed的作用

    sealed 修饰符表示密封 用于类时,表示该类不能再被继承,不能和 abstract 同时使用,因为这两个修饰符在含义上互相排斥 用于方法和属性时,表示该方法或属性不能再被重写,必须和 overri ...

  3. informix 存储过程结构

    1.简介         存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中.用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行 ...

  4. PhotoSwipe简介

    官方网站http://www.photoswipe.com/源码示例http://github.com/downloads/codecomputerlove/PhotoSwipe/code.photo ...

  5. Github命令详解

    Git bash: ***创建本地版本库: $ cd d: $ mkdir git $ cd git $ mkdir test $ git init   //初始化本地库 ***创建文件并添加到版本库 ...

  6. python多进程,以及进程池并发

    模拟多进程 #!/usr/bin/env python#-*- coding:utf-8 -*-import timefrom multiprocessing import Process def s ...

  7. SQL MySQL

    SQL 结构化查询语言(英语:Structural Query Language,缩写:SQL),是一种特殊目的之编程语言,用于数据库中的标准数据查询语言. 各种通行的数据库系统在其实践过程中都对SQ ...

  8. hbase自带mapreduce计数表行数功能

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename’ mapreduce来计数,很快的!!!

  9. icon的使用

    在前端页面设计时,不免使用的就是图标,下面就我使用图标icon分享一下经验 1.icon插件,现在比较好的是bootstrap自带的,fontawesome,链接地址:http://fontaweso ...

  10. XPath相关笔记

    <?xml version="1.0" encoding="utf-8" ?> <employees>   <employee o ...