1.使用数据模板对TreeViewItem进行更改

2.xaml中重写TreeviewItem的控件模板

3.继承TreeViewItem(TreeView中的元素),后台进行控件重写。(介绍此方法)

代码:

  1.      ImageSource iconSource;//图标对象
  2. TextBlock textBlock;
  3. Image icon;//treeviewitem中的图标
  4.  
  5. public TreeViewWithIcons()
  6. {
  7. StackPanel stack = new StackPanel();
  8. stack.Orientation = Orientation.Horizontal;
  9. Header = stack;
  10. //Uncomment this code If you want to add an Image after the Node-HeaderText
  11. //textBlock = new TextBlock();
  12. //textBlock.VerticalAlignment = VerticalAlignment.Center;
  13. //stack.Children.Add(textBlock);
  14. icon = new Image();
  15. icon.VerticalAlignment = VerticalAlignment.Center;
  16. icon.Height = ;
  17. icon.Width = ;
  18. icon.Margin = new Thickness(, , , );
  19. icon.Source = iconSource;
  20. stack.Children.Add(icon);
  21. //Add the HeaderText After Adding the icon
  22. textBlock = new TextBlock();
  23. textBlock.VerticalAlignment = VerticalAlignment.Center;
  24. stack.Children.Add(textBlock);
  25. }

将TreeviewItem的header设置成stackpanel,将图标和文本以横向填充进该stackpanel

完整代码:

  1. public class TreeViewWithIcons : TreeViewItem
  2. {
  3. ImageSource iconSource;
  4. TextBlock textBlock;
  5. Image icon;
  6.  
  7. public TreeViewWithIcons()
  8. {
  9. StackPanel stack = new StackPanel();
  10. stack.Orientation = Orientation.Horizontal;
  11. Header = stack;
  12. //Uncomment this code If you want to add an Image after the Node-HeaderText
  13. //textBlock = new TextBlock();
  14. //textBlock.VerticalAlignment = VerticalAlignment.Center;
  15. //stack.Children.Add(textBlock);
  16. icon = new Image();
  17. icon.VerticalAlignment = VerticalAlignment.Center;
  18. icon.Height = ;
  19. icon.Width = ;
  20. icon.Margin = new Thickness(, , , );
  21. icon.Source = iconSource;
  22. stack.Children.Add(icon);
  23. //Add the HeaderText After Adding the icon
  24. textBlock = new TextBlock();
  25. textBlock.VerticalAlignment = VerticalAlignment.Center;
  26. stack.Children.Add(textBlock);
  27. }
  28.  
  29. public ImageSource Icon
  30. {
  31. set
  32. {
  33. iconSource = value;
  34. icon.Source = iconSource;
  35. }
  36. get
  37. {
  38. return iconSource;
  39. }
  40. }
  41.  
  42. protected override void OnUnselected(RoutedEventArgs args)
  43. {
  44. base.OnUnselected(args);
  45. icon.Source = iconSource;
  46. }
  47.  
  48. protected override void OnSelected(RoutedEventArgs args)
  49. {
  50. base.OnSelected(args);
  51. icon.Source = iconSource;
  52. }
  53.  
  54. /// <summary>
  55. /// Gets/Sets the HeaderText of TreeViewWithIcons
  56. /// </summary>
  57. public string HeaderText
  58. {
  59. set
  60. {
  61. textBlock.Text = value;
  62. }
  63. get
  64. {
  65. return textBlock.Text;
  66. }
  67. }
  68. }

c# WPF——创建带有图标的TreeView的更多相关文章

  1. WPF 带CheckBox、图标的TreeView

    WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提 ...

  2. WPF 带CheckBox、图标的TreeView(转)

    在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提供强大的ItemTemplate模板功能和自定义样式 ...

  3. 带有图标的MessageBox

    MessageBoxUtils类的代码如下: Ext.define('org.pine.util.MessageBoxUtils', { singleton: true, /** 普通信息提示框 */ ...

  4. 使用StarUML创建类图

    使用StarUML创建类图 http://www.flyne.org/article/379 1.综述(What) StarUML是一种生成类图和其他类型的UML图表的工具.本文是一个使用StarUM ...

  5. 利用WPF创建含多种交互特性的无边框窗体

    咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...

  6. 如何用Rational rose创建类图

    UML中各种图形概要: 图名 对照 说明 用例图 use case diagram 用例图表明系统做什么,与谁交互.用例是系统提供的功能,参与者是系统与谁交互,参与者可以是人.系统或其他实体.一个系统 ...

  7. 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据

    原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...

  8. Qt 怎样生成带图标的exe

    一.问题描述 当我们在 Windows 下用 VS 生成 exe 程序时,如果窗口程序指定了图标,那么生成的 exe 程序便是指定的图标模样. 但是,当使用 Qt Creator 编译程序却不同.即使 ...

  9. 【百度地图API】如何快速创建带有标注的地图?——快速创建地图工具+如何标注商家

    原文:[百度地图API]如何快速创建带有标注的地图?--快速创建地图工具+如何标注商家 摘要: 如果你不会程序,如果你不想写代码. 如果你想拥有一张自己的地图,如果你想在该地图上标注出你商店的位置. ...

随机推荐

  1. jquery data的用法

    jquery data和 jquery attr, js getAttribute 有着本质的区别,并且无法用$(el).data('property')的方法,去获取$(el).attr('data ...

  2. vue项目1-pizza点餐系统10-axios实现登陆

    一.登陆思路 从数据库中拿到数据,每个用户信息都是一个对象,需要用数组存储对象信息,然后对比输入的用户信息,如果匹配成功就跳转到home主页,否则提示用户或密码错误. 二.具体操作 1.导入axios ...

  3. console 对象

    JavaScript 原生中默认是没有 Console 对象,这是宿主对象(也就是游览器)提供的内置对象. 用于访问调试控制台,在不同的浏览器里效果可能不同.Console 对象方法:

  4. 32. Longest Valid Parentheses (JAVA)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. Spring基础09——Bean的自动装配

    1.XML配置的Bean自动装配 SpringIOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,而不需要手工去指定要装配的Bean,a ...

  6. 关于tomcat NoClassDefDoundErr异常的记录

    在做DRP项目的时候,copy了drp1.3,粘贴重命名成drp1.4,把drp1.4加入到tomcat中,发现drp1.4中新加的jsp可以正常运行,而从1.3那copy来的不能运行,抛出NoCla ...

  7. PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)

    This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...

  8. nginx_mirror_module流量复制在项目中的应用

    参考文档:https://my.oschina.net/andChow/blog/2873870 https://blog.csdn.net/lancerh/article/details/88645 ...

  9. linux 时区问题

    1.java项目发现 服务器时间不正确,修改了服务器时间之后依然没解决. 2.java虚拟机的时区也需要设置:

  10. Flask【第3篇】:蓝图、基于DBUtils实现数据库连接池、上下文管理等

    基于DBUtils实现数据库连接池 小知识: 1.子类继承父类的三种方式 class Dog(Animal): #子类 派生类 def __init__(self,name,breed, life_v ...