记录一些ListBox的用法

1.设置ListBox选中项的背景颜色

  • 采用模板

先看一下效果图:

在设置listbox选中样式是遇到一个问题:选中项的背景设置不起作用

经过一段时间的挣扎后找到原因,模板里的控件要设置 Background="{TemplateBinding Background}" TextBlock.Foreground="{TemplateBinding Foreground}"否则背景是不会变化的。

  1. <ListBox Name="list" ItemsSource="{Binding InfoList}">
  2. <ListBox.ItemContainerStyle>
  3. <Style TargetType="ListBoxItem">
  4. <!-- 设置控件模板 -->
  5. <Setter Property="Template">
  6. <Setter.Value>
  7. <ControlTemplate TargetType="ListBoxItem">
  8. <Grid>
  9. <Grid.ColumnDefinitions>
  10. <ColumnDefinition Width=""/>
  11. <ColumnDefinition Width="*"/>
  12. <ColumnDefinition Width="auto"/>
  13. </Grid.ColumnDefinitions>
  14. <TextBlock Text="{Binding Num}" Grid.Column=""/>
  15. <TextBlock Text="{Binding Name}" Grid.Column="" Background="{TemplateBinding Background}" TextBlock.Foreground="{TemplateBinding Foreground}"/>
  16. <TextBlock Text="{Binding Sex}" Grid.Column=""/>
  17. </Grid>
  18. </ControlTemplate>
  19. </Setter.Value>
  20. </Setter>
  21.  
  22. <!-- 设置触发器 -->
  23. <Style.Triggers>
  24. <Trigger Property="IsSelected" Value="true">
  25. <Setter Property="Background" Value="#64BCEA"/>
  26. </Trigger>
  27. <Trigger Property="IsMouseOver" Value="true">
  28. <Setter Property="Background" Value="#C7DEEA"/>
  29. <!--<Setter Property="Foreground" Value="Red"/>-->
  30. </Trigger>
  31. </Style.Triggers>
  32. </Style>
  33. </ListBox.ItemContainerStyle>
  34. </ListBox>

设置中间列有效果

  1. <ListBox Name="list" ItemsSource="{Binding InfoList}">
  2. <ListBox.ItemContainerStyle>
  3. <Style TargetType="ListBoxItem">
  4. <!-- 设置控件模板 -->
  5. <Setter Property="Template">
  6. <Setter.Value>
  7. <ControlTemplate TargetType="ListBoxItem">
  8. <Grid Background="{TemplateBinding Background}" TextBlock.Foreground="{TemplateBinding Foreground}">
  9. <Grid.ColumnDefinitions>
  10. <ColumnDefinition Width=""/>
  11. <ColumnDefinition Width="*"/>
  12. <ColumnDefinition Width="auto"/>
  13. </Grid.ColumnDefinitions>
  14. <TextBlock Text="{Binding Num}" Grid.Column=""/>
  15. <TextBlock Text="{Binding Name}" Grid.Column=""/>
  16. <TextBlock Text="{Binding Sex}" Grid.Column=""/>
  17. </Grid>
  18.  
  19. </ControlTemplate>
  20. </Setter.Value>
  21. </Setter>
  22.  
  23. <!-- 设置触发器 -->
  24. <Style.Triggers>
  25. <Trigger Property="IsSelected" Value="true">
  26. <Setter Property="Background" Value="#64BCEA"/>
  27. </Trigger>
  28. <Trigger Property="IsMouseOver" Value="true">
  29. <Setter Property="Background" Value="#C7DEEA"/>
  30. <!--<Setter Property="Foreground" Value="Red"/>-->
  31. </Trigger>
  32. </Style.Triggers>
  33. </Style>
  34. </ListBox.ItemContainerStyle>
  35. </ListBox>

设置整个ListBoxItem有效果

里面我用到的布局控件为“Grid”简单点的还可以用“Border”等控件。

  • 自定义SystemColors类的参数

SystemColors的HighlightBrushKey和HighlightTextBrushKey分别代表ListBoxItem被选中时文字和背景颜色,没有Highlight的BrushKey代表ListBox没有焦点时的选中项文字和背景颜色。

效果图:

  1. <ListBox>
  2. <ListBox.Resources>
  3. <Style TargetType="ListBoxItem">
  4. <Style.Resources>
  5. <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Pink"/>
  6. <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
  7. <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
  8. <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Green"/>
  9. </Style.Resources>
  10. </Style>
  11. </ListBox.Resources>
  12. <ListBoxItem>AAA</ListBoxItem>
  13. <ListBoxItem>B</ListBoxItem>
  14. <ListBoxItem>ccc</ListBoxItem>
  15. </ListBox>

代码示例

这是在网上其他人的博客里看到的,我没有详细去做。

2.如何为标准的ListBox添加ItemClick事件

  1. public class MyListBox:ListBox
  2. {
  3. private static readonly object EventItemClick = new object();
  4. public event EventHandler<ListBoxItemEventArgs> ItemClick
  5. {
  6. add
  7. {
  8. Events.AddHandler(EventItemClick, value);
  9. }
  10. remove
  11. {
  12. Events.RemoveHandler(EventItemClick, value);
  13. }
  14. }
  15.  
  16. protected virtual void OnItemClick(ListBoxItemEventArgs e)
  17. {
  18. EventHandler<ListBoxItemEventArgs> handler = (EventHandler<ListBoxItemEventArgs>)this.Events[EventItemClick];
  19. if (handler != null)
  20. {
  21. handler(this, e);
  22. }
  23. }
  24.  
  25. protected override void OnClick(EventArgs e)
  26. {
  27. base.OnClick(e);
  28. for (int i = ; i < this.Items.Count; i++)
  29. {
  30. bool flag = this.GetItemRectangle(i).Contains(this.PointToClient(Control.MousePosition));
  31. if (flag)
  32. {
  33. ListBoxItemEventArgs args = new ListBoxItemEventArgs(i);
  34. OnItemClick(args);
  35. break;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. public class ListBoxItemEventArgs : EventArgs
  42. {
  43. private int _listBoxItem;
  44.  
  45. public ListBoxItemEventArgs(int listBoxItem)
  46. {
  47. _listBoxItem = listBoxItem;
  48. }
  49.  
  50. public int ListBoxItem
  51. {
  52. get
  53. {
  54. return _listBoxItem;
  55. }
  56. }
  57. }

3.连续选择同一项时SelectionChanged 事件不响应的问题

  • 简单点的方式

处理SelectionChanged当操作结束后把SelectedIndex设为-1;

  1. private void lBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  2. {
  3. if (lBox.SelectedIndex == -)
  4. {
  5. return;
  6. }
  7. // 这里填写所需要处理的代码
  8. lBox.SelectedIndex = -;
  9. }
  • 复杂点就是重载OnSelect()

这不局限与ListBox,TreeView等都可以参考

  1. public class myListBox : System.Windows.Controls.ListBox
  2. {
  3. protected override DependencyObject GetContainerForItemOverride()
  4. {
  5. return new myListBoxItem();
  6. }
  7.  
  8. }
  9. public class myListBoxItem : System.Windows.Controls.ListBoxItem
  10. {
  11. protected override void OnSelected(System.Windows.RoutedEventArgs e)
  12. {
  13. DependencyObject dep = (DependencyObject)e.OriginalSource;
  14.  
  15. while ((dep != null) && !(dep is ListBoxItem))
  16. {
  17. dep = VisualTreeHelper.GetParent(dep);
  18. }
  19.  
  20. if (dep == null)
  21. return;
  22.  
  23. ListBoxItem item = (ListBoxItem)dep;
  24.  
  25. if (item.IsSelected)
  26. {
  27. item.IsSelected = !item.IsSelected;
  28. //e.Handled = true;
  29. }
  30. base.OnSelected(e);
  31. }
  32. }

这是我在一个博客上转载的,不过当时放在有道云笔记中,作者的地址已经丢失,在此对这位博主表示歉意。

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. MySQL设置字符集为UTF8(Windows版)

    Windows版MySQL设置字符集全部为utf8的方式 MySQL安装目录下的my.ini文件 [client]节点 default-character-set=utf8    (增加) [mysq ...

  2. 如何理解meta标签

    相信大家对meta标签都不陌生,但是打开某些网页源代码看到一堆的meta难免一脸蒙逼.废话少说,请大家更随我一起探索meta标签都有哪些属性以及作用. W3C给meta标签的定义是:<meta& ...

  3. sumoselect插件

    由于项目需要,研究了下sumoselect插件,接下来简单介绍下sumoselect. 在百度上搜索“sumoselect.js”,查到的网页基本上都有对sumoselect的基本介绍,如下: 简单介 ...

  4. C#基础——系统登录功能的实现

    一般的登陆界面,都是利用用户名和密码在数据库的匹配关系,来实现登陆的跳转功能. 首先介绍用户数据表的设计. 其中ID列需要设置好增量标识,随着用户的增加,ID的值递增,避免重复. 然后是C#中对数据库 ...

  5. RBM阅读笔记

    RBM包含两个层,可见层(visble layer)和隐藏层(hidden layer).神经元之间的连接具有以下特点:层内无连接,层间全连接.RBM可以看做是一个二分图(神经元当做顶点,神经元之间的 ...

  6. Android之ProgressBar初步应用

    这里利用 ProgressBar 即时显示下载进度. 途中碰到的问题: 1.主线程中不能打开 URL,和只能在主线程中使用 Toast 等 2.子线程不能修改 UI 3.允许网络协议 4.暂停下载和继 ...

  7. 4-Spark高级数据分析-第四章 用决策树算法预测森林植被

    预测是非常困难的,更别提预测未来. 4.1 回归简介 随着现代机器学习和数据科学的出现,我们依旧把从“某些值”预测“另外某个值”的思想称为回归.回归是预测一个数值型数量,比如大小.收入和温度,而分类则 ...

  8. div各种距离 详细解释图

    详细博文介绍:http://blog.csdn.net/fswan/article/details/17238933

  9. C++ STL泛型编程——在ACM中的运用

    学习过C++的朋友们应该对STL和泛型编程这两个名词不会陌生.两者之间的关系不言而喻,泛型编程的思想促使了STL的诞生,而STL则很好地体现了泛型编程这种思想.这次想简单说一下STL在ACM中的一些应 ...

  10. DMA控制器

    DMA控制器依赖于平台硬件,这里只对i386的8237 DMA控制器做简单的说明,它有两个控制器,8个通道,具体说明如下: 控制器1: 通道0-3,字节操作, 端口为 00-1F 控制器2: 通道 4 ...