[WPF系列]-ListBox
引言
本文就WPF中的ListBox常用项给以实例代码演示,包括隐蔽属性的设置,Style设置,以及ControlTemplate的自定义。
Listbox平滑滚动
<ListBox ItemsSource="{Binding ActorList}" Width="300"
ScrollViewer.CanContentScroll="False"/>
或者
<ListBox.Template>
<ControlTemplate>
<ScrollViewer Focusable="False" CanContentScroll="True">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
参考:http://stackoverflow.com/a/3031298/1616023
StackPanel implements the IScrollInfo interface to do logical scrolling, but if ScrollViewer can't find an IScrollInfo child it falls back to doing physical scrolling.
There are three ways to obtain logical scrolling inside a ScrollViewer:
- Let the ScrollViewer's direct child be a panel that can do logical scrolling (such as a StackPanel)
- Let the ScrollViewer's direct child be an ItemsPresenter which presents such a panel
- Let the ScrollViewer's direct child be a custom class you write yourself that implements IScrollInfo
点击ListboxItem任意位置选中ListBoxItem
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
引用:Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area
控制滚动条的显示
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
更改Items的container
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
或者
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Style>
自定义ItemsControl template
<ItemsControl x:Name="activitiesControl" Margin="10">
<ItemsControl.Template>
<ControlTemplate>
<WrapPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{DynamicResource ActionButton}" HorizontalAlignment="Right" Margin="5"
Content="{Binding Value}" Width="200"
Command="{Binding Path=ViewModel.ActionTypeCommand,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=local:CustomerEditView}}" CommandParameter="{Binding Key}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
参考另一文章数据邦定之DataTemplate 根据对象属性切换模板
自定ListBox
A WPF Problem Solved Two Very Different Ways - Using XAML Only - Using a Custom Control
Listbox 为空时显示
利用AdornerLayer实现如下:
EmptyItemsControlOverlay
另一简单办法:
<Style TargetType="{x:Type ListBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items.Count,
RelativeSource={RelativeSource Self}}" Value="0">
<Setter Property="Background">
<Setter.Value>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<TextBlock Text="No Items" VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="False" />
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
<!--方法二-->
<!--<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="1" BorderBrush="Black"
Padding="10" Margin="10" Background="Transparent">
--><!--<TextBlock>No items to display</TextBlock>--><!--
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>-->
</DataTrigger>
</Style.Triggers>
</Style>
参考
WPF : Using Adorner for overlaying Empty Collections
[WPF系列]-ListBox的更多相关文章
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- [WPF系列]-数据邦定之DataTemplate 对分层数据的支持
到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...
- [WPF系列]-TreeView的常用事项
引言 项目经常会用Treeview来组织一些具有层级结构的数据,本节就将项目使用Treeview常见的问题作一个总结. DataBinding数据绑定 DataTemplate自定义 <Hier ...
- [WPF系列]从基础起步学习系列计划
引言 WPF技术已经算不什么新技术,一搜一大把关于WPF基础甚至高级的内容.之前工作中一直使用winform所以一直没有深入学习WPF,这次因项目中使用了WPF技术来实现比较酷的展示界面.我在这里只是 ...
- WPF ItemsControl ListBox ListView比较
在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表 ...
- C# WinForm开发系列 - ListBox/ListView/Panel
转自会飞的小猪文章 C# WinForm开发系列 - ListBox/ListView/Panel 在博客园看到了一篇博文,觉得很不错,就转载过来了. 包含自定义绘制的ListBox, 带拖动, ...
- WPF中ListBox的项ListBoxItem被选中的时候Background变化
使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:
- WPF系列教程——(三)使用Win10 Edge浏览器内核 - 简书
原文:WPF系列教程--(三)使用Win10 Edge浏览器内核 - 简书 在需要显示一些 H5网站的时候自带的WebBrowser总是显示不了,WebBrowser使用的是IE内核,许多H5新特性都 ...
- WPF系列教程——(一)仿TIM QQ界面 - 简书
原文:WPF系列教程--(一)仿TIM QQ界面 - 简书 TIM QQ 我们先来看一下TIM QQ长什么样,整体可以将界面分为三个部分 TIM QQ 1. 准备 阅读本文假设你已经有XAML布局的基 ...
随机推荐
- Genesis2000用c#开发外挂
先上官方的说明 gateway is a command line utility for sending messages and commands to Genesis processes. Th ...
- C#中实现对象间的更新操作
最近工作的时候遇到一个问题,根据Web端接收到的对象obj1,更新对应的对象值ogj2.先判断obj1中属性值是否为null, 若不等于null,则更新obj2中对应属性值:若等于null,则保持ob ...
- iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)
iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器 实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...
- oracle与sqlserver部分区别
oracle和sqlserver的区别:1,执行修改操作要接commit,不然数据仅仅只是查看,并不是提交数据2,oracle不能使用select 字段 这种查看方式查看数据:3,oracle存储过程 ...
- 错误:下列软件包有未满足的依赖关系: openssh-server : 依赖: openssh-client (= 1:7.1p1-4)
解决办法:尝试了很久才解决,这个是我发现最有用的,完美的解决了我的困难 后续过程参考:http://blog.csdn.net/jszhangyili/article/details/8881807 ...
- linux下安装mysql
下载Mysql包 因为mysql比较大,我们不能像安装nginx和php那样,通过下载源码,编译成二进制安装.mysql安装比php和nginx稍微麻烦一点. 这里mysql我们直接下载编译好的二进制 ...
- 工业串口和网络软件通讯平台(SuperIO 2.0)发布
下载:SuperIO 2.0(开发手册.Demo.组件包) 官网:进入 交流群:54256083 SuperIO 2.0版本正式发布.把SCL正式更改名称为SuperIO. 一.此次升级主要包括两个方 ...
- 使用javascript生成的植物显示过程特效
查看效果:http://keleyi.com/keleyi/phtml/html5/33.htm .NET版本:http://keleyi.com/a/bjac/66mql4bc.htm 完整HTML ...
- npm 初学者教程
Node.js 让 JavaScript 编写服务器端应用程序成为可能.它建立在 JavaScript V8(C++ 编写的) 运行时之上,所以它很快.最初,它旨在为应用程序提供服务器环境,但是开发人 ...
- 安全生产应急救援指挥系统之GIS一张图-flex/java
开发语言是flex.java,开发平台是myeclise.eclise,后台数据库是oracel或sqlserver,开发接口是arcgis api for flex,提供以下的功能: 1.地图框选搜 ...