记录一些ListBox的用法

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的更多相关文章

  1. 自定义WPF ListBox的选中项样式

    首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBrushKey和HighlightText ...

  2. WPF ListBox数据绑定

    本文来源 http://wshoufeng1989.blog.163.com/blog/static/202047033201282911633670/  风随影动的博客 使用数据库AllData , ...

  3. WPF : ListBox的几种Template属性

    原文:WPF : ListBox的几种Template属性 属性名 属性的类名 功能 示例 Template ControlTemplate 定义控件自身的外观.其子元素的布局可以自定义,也可以由It ...

  4. 自定义WPF ListBox的选择样式

    (下图:进行多项选择的ListBox) 首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBr ...

  5. wpf listbox 选中项 上移下移

    原文:wpf listbox 选中项 上移下移 private void MoveUp_Click(object sender, RoutedEventArgs e)         {        ...

  6. WPF ListBox 横向排列

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

  7. Wpf ListBox数据绑定实例1--绑定字典集合

    1.使用ListBox绑定Dictionary字典数据 ListBox常用事件SelectionChanged private void bindListBox() { Dictionary<s ...

  8. WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式

    ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...

  9. WPF ListBox ItemContainerStyle 设置BackGround 和 BorderBrush 无效

    今天更改ListBox,用到ItemContainerStyle设置样式,设置Style.Triggers时,BackGround和BorderBrush均无效,其他效果正常. 翻看WPF编程宝典,发 ...

随机推荐

  1. ARM9的中断控制器

    简要复习一下ARM9中断控制器的控制过程: 1.首先能识别触发的中断(对应中断源必须打开,然后查询当前中断状态寄存器),硬件会操控PC跳到中断向量入口(IRQ_HANDLE,硬件控制的只要是IRQ中断 ...

  2. 实时控制软件设计 第二次作业 myRobot

    #include<iostream> #include <Eigen/Dense> #include <math.h> #include <vector> ...

  3. codeforces 651C(map、去重)

    题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...

  4. Spring面向切面之AOP深入探讨

    Spring之AOP深入探讨 刚接触AOP之前我已经找了网上各种博客论坛上的关于AOP的文章利于我理解因为听好多人说AOP很复杂,很深奥当我接触之后发现根本不是那么的难于理解.它只是一个基于OOP技术 ...

  5. svn/git的diff、patch

    svn/git的diff.patch 前几天,正当我突突的写代码,企业微信嘀嘀一声响”在不,过来帮我看个bug”.本人一向助人为乐,高兴的冲了过去,然后就开始了一段长达1分钟的问题描述.很明显,此同学 ...

  6. ThinkPHP 自动验证相关注意

    1.假如加入了表单令牌的话,表单的各种名与对应Model字段一致,不然报坑爹的令牌错误. 2.假如加入了表单令牌的话,Create只能采用默认的POST数据创建数据对象,不然又是坑爹的令牌错误. 3. ...

  7. mysql用户名密码忘记了解决方法

    今天想用一下实验室服务器的mysql,发现不记得用户名密码了. 解决方法如下: 1. 保证服务器处于安全的状态,如果可以请拔掉网线...(不过我跳过了这一步,额) 2. 修改/etc/my.cnf文件 ...

  8. Linux手动释放缓存的方法

    Linux释放内存的命令:syncecho 1 > /proc/sys/vm/drop_caches drop_caches的值可以是0-3之间的数字,代表不同的含义:0:不释放(系统默认值)1 ...

  9. Android RadioButton 语言无法切换问题

    1.Dialog在不退出界面的情况下,RadioButton在语言切换时,无法匹配系统语言的问题: 解决办法为:在RadioButton添加属性 android:saveEnabled="f ...

  10. MINIX3

    这个系列minix3是好早看的源码  现在都忘记的差不多了 觉得就此扔掉可惜了  今天把他全部放在博客上 1 是想和大家一起讨论下 2 是没事看看 能够加强对一个稳定性系统的理解 加厚