WPF ListBox
记录一些ListBox的用法
- 设置ListBox选中项的背景颜色
- 如何为标准的ListBox添加ItemClick事件
- 连续选择同一项时SelectionChanged 事件不响应的问题
1.设置ListBox选中项的背景颜色
- 采用模板
先看一下效果图:


在设置listbox选中样式是遇到一个问题:选中项的背景设置不起作用
经过一段时间的挣扎后找到原因,模板里的控件要设置 Background="{TemplateBinding Background}" TextBlock.Foreground="{TemplateBinding Foreground}"否则背景是不会变化的。
<ListBox Name="list" ItemsSource="{Binding InfoList}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<!-- 设置控件模板 -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Num}" Grid.Column=""/>
<TextBlock Text="{Binding Name}" Grid.Column="" Background="{TemplateBinding Background}" TextBlock.Foreground="{TemplateBinding Foreground}"/>
<TextBlock Text="{Binding Sex}" Grid.Column=""/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- 设置触发器 -->
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="#64BCEA"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#C7DEEA"/>
<!--<Setter Property="Foreground" Value="Red"/>-->
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
设置中间列有效果
<ListBox Name="list" ItemsSource="{Binding InfoList}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<!-- 设置控件模板 -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}" TextBlock.Foreground="{TemplateBinding Foreground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Num}" Grid.Column=""/>
<TextBlock Text="{Binding Name}" Grid.Column=""/>
<TextBlock Text="{Binding Sex}" Grid.Column=""/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- 设置触发器 -->
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="#64BCEA"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#C7DEEA"/>
<!--<Setter Property="Foreground" Value="Red"/>-->
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
设置整个ListBoxItem有效果
里面我用到的布局控件为“Grid”简单点的还可以用“Border”等控件。
- 自定义SystemColors类的参数
SystemColors的HighlightBrushKey和HighlightTextBrushKey分别代表ListBoxItem被选中时文字和背景颜色,没有Highlight的BrushKey代表ListBox没有焦点时的选中项文字和背景颜色。
效果图:

<ListBox>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Pink"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Green"/>
</Style.Resources>
</Style>
</ListBox.Resources>
<ListBoxItem>AAA</ListBoxItem>
<ListBoxItem>B</ListBoxItem>
<ListBoxItem>ccc</ListBoxItem>
</ListBox>
代码示例
这是在网上其他人的博客里看到的,我没有详细去做。
2.如何为标准的ListBox添加ItemClick事件
public class MyListBox:ListBox
{
private static readonly object EventItemClick = new object();
public event EventHandler<ListBoxItemEventArgs> ItemClick
{
add
{
Events.AddHandler(EventItemClick, value);
}
remove
{
Events.RemoveHandler(EventItemClick, value);
}
} protected virtual void OnItemClick(ListBoxItemEventArgs e)
{
EventHandler<ListBoxItemEventArgs> handler = (EventHandler<ListBoxItemEventArgs>)this.Events[EventItemClick];
if (handler != null)
{
handler(this, e);
}
} protected override void OnClick(EventArgs e)
{
base.OnClick(e);
for (int i = ; i < this.Items.Count; i++)
{
bool flag = this.GetItemRectangle(i).Contains(this.PointToClient(Control.MousePosition));
if (flag)
{
ListBoxItemEventArgs args = new ListBoxItemEventArgs(i);
OnItemClick(args);
break;
}
}
}
} public class ListBoxItemEventArgs : EventArgs
{
private int _listBoxItem; public ListBoxItemEventArgs(int listBoxItem)
{
_listBoxItem = listBoxItem;
} public int ListBoxItem
{
get
{
return _listBoxItem;
}
}
}
3.连续选择同一项时SelectionChanged 事件不响应的问题
- 简单点的方式
处理SelectionChanged当操作结束后把SelectedIndex设为-1;
private void lBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lBox.SelectedIndex == -)
{
return;
}
// 这里填写所需要处理的代码
lBox.SelectedIndex = -;
}
- 复杂点就是重载OnSelect()
这不局限与ListBox,TreeView等都可以参考
public class myListBox : System.Windows.Controls.ListBox
{
protected override DependencyObject GetContainerForItemOverride()
{
return new myListBoxItem();
} }
public class myListBoxItem : System.Windows.Controls.ListBoxItem
{
protected override void OnSelected(System.Windows.RoutedEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is ListBoxItem))
{
dep = VisualTreeHelper.GetParent(dep);
} if (dep == null)
return; ListBoxItem item = (ListBoxItem)dep; if (item.IsSelected)
{
item.IsSelected = !item.IsSelected;
//e.Handled = true;
}
base.OnSelected(e);
}
}
这是我在一个博客上转载的,不过当时放在有道云笔记中,作者的地址已经丢失,在此对这位博主表示歉意。
WPF ListBox的更多相关文章
- 自定义WPF ListBox的选中项样式
首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBrushKey和HighlightText ...
- WPF ListBox数据绑定
本文来源 http://wshoufeng1989.blog.163.com/blog/static/202047033201282911633670/ 风随影动的博客 使用数据库AllData , ...
- WPF : ListBox的几种Template属性
原文:WPF : ListBox的几种Template属性 属性名 属性的类名 功能 示例 Template ControlTemplate 定义控件自身的外观.其子元素的布局可以自定义,也可以由It ...
- 自定义WPF ListBox的选择样式
(下图:进行多项选择的ListBox) 首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBr ...
- wpf listbox 选中项 上移下移
原文:wpf listbox 选中项 上移下移 private void MoveUp_Click(object sender, RoutedEventArgs e) { ...
- WPF ListBox 横向排列
WPF ListBox 横向排列 如果只是单纯的让ListBox可以横向配列,这样很简单,只需要更改ListBox的ItemsPanel模板就可以,例如: <ListBox><L ...
- Wpf ListBox数据绑定实例1--绑定字典集合
1.使用ListBox绑定Dictionary字典数据 ListBox常用事件SelectionChanged private void bindListBox() { Dictionary<s ...
- WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式
ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...
- WPF ListBox ItemContainerStyle 设置BackGround 和 BorderBrush 无效
今天更改ListBox,用到ItemContainerStyle设置样式,设置Style.Triggers时,BackGround和BorderBrush均无效,其他效果正常. 翻看WPF编程宝典,发 ...
随机推荐
- 算法(第4版)-1.5 案例研究:union-find算法
问题→ 动态连通性:当程序从输入中读取了整数对p q时,如果已知的所有整数对都不能说明p和q是相连的,那么则将这一对整数写入到输出中.如果已知的数据可以说明p和q 是相连的,那么程序应该忽略p q这对 ...
- html+css知识点总结(田彦霞)
html部分 html头部声明 DOCTYPE是document type(文档类型)的简写,用来说明你用的XHTML或者HTML是什么版本.DOCTYPE声明必须放在每一个XHTML文档最顶部,在所 ...
- NOIP 考前 暴力练习
BZOJ 1028 暴力枚举听的那张牌,和那个多余的两张牌,其余的mod3后模拟就可以了 #include <cstdio> ; int n,m,a[Maxn],b[Maxn],cnt,A ...
- oracle xmltype导入并解析Excel数据 (二)规则说明
规则表字段说明如下: 其中RULE_FUNC_CUSTOMIZE表示,用户自己写函数,去判断数据是否合法,存储的是函数的名字 此函数的参数只有一个,该列的值,字段类型是Varchar2, 校验失败的话 ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- Html 之菜单导航(二)
网页菜单 网页菜单是一个网页的重要部分,它提供了用户可以对网站所有页面的导航,也是可能是内容的分类,例如淘宝 衣服 鞋子 水果 电脑 等等之类,也可以是 类似于游戏宣传网页一样子,酷炫的js特效 f ...
- codeforces716E (点分治)
Problem Digit Tree 题目大意 给一棵树,有边权1~9. 询问有多少个点对(i,j),将i--j路径上的数字依次连接后所形成新数字可以被k整除.gcd(K,10)=1 解题分析 点分治 ...
- java中与数据库的连接
package unitl01; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet; ...
- Error: unable to connect to node rabbit@mail: nodedown
某天,开启一个应用时,发现连接rabbitmq失败,本来想用rabbitmqctl来查看队列,结果提示“Error: unable to connect to node rabbit@mail: no ...
- winform客户端利用webClient实现与Web服务端的数据传输
由于项目需要,最近研究了下WebClient的数据传输.关于WebClient介绍网上有很多详细介绍,大概就是利用WebClient可以实现对Internet资源的访问.无外乎客户端发送请求,服务端处 ...