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. 链表中LinkList L与LinkList *L 借鉴

    链表中LinkList L与LinkList *L的区别以及(*L).elem,L.elem L->next,(*L)->next的区别typedef struct Node{int el ...

  2. Tomcat多域名的配置

    有时候我们有好几个项目需要发布在同一个tomcat服务器上,每个项目有不同的域名.这就需要在tomcat里配置多域名,添加多个虚拟主机. 主要在server.xml里面设置: 在<Engine& ...

  3. php笔记(二)PHP类和对象之Static静态关键字

      PHP类和对象之Static静态关键字 静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用. class Car { pr ...

  4. 多线程随笔一(AutoResetEvent和ManulResetEvent)

    AutoResetEvent和ManulResetEvent是.net中用来做线程的同步的两个类.从类名上不难看出,这两个类的区别在于一个是自动的,一个是手动的(这不是废话嘛).两个类的实现原理大致相 ...

  5. bootstrap页面模板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. HDU 5839 Special Tetrahedron

    HDU 5839 Special Tetrahedron 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5839 Description Given n ...

  7. 用SqlBulkCopy批量插入数据到SqlServer数据库表中

    首先创建一个数据库连接类:SQLHelper using System; using System.Collections.Generic; using System.Linq; using Syst ...

  8. react学习笔记-01

    1. HTML模板 Jsx是react的语法糖,最终会被编译成js语法.因此需要第三方库browser将jsx转换成js. 由于react 0.14版本之后,将react和react-dom拆分,所以 ...

  9. android的签名

    安装好了android studio,默认是使用期限为一年的签名,并且不可以发布到正式版的apk里. 在使用第三方模块或者服务的时候,经常要求提供签名及其sha1或者MD5信息. 事实上这个签名和及其 ...

  10. 如何设置linux的文件和目录的权限

    1 字符表示法 1)chmod [-R] 权限(mode)file   注:[-R]会将目录下所有权限都设置成指定的 who operator permission u(owner) +增加权限 r( ...