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

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

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

代码:

     ImageSource iconSource;//图标对象
TextBlock textBlock;
Image icon;//treeviewitem中的图标 public TreeViewWithIcons()
{
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
Header = stack;
//Uncomment this code If you want to add an Image after the Node-HeaderText
//textBlock = new TextBlock();
//textBlock.VerticalAlignment = VerticalAlignment.Center;
//stack.Children.Add(textBlock);
icon = new Image();
icon.VerticalAlignment = VerticalAlignment.Center;
icon.Height = ;
icon.Width = ;
icon.Margin = new Thickness(, , , );
icon.Source = iconSource;
stack.Children.Add(icon);
//Add the HeaderText After Adding the icon
textBlock = new TextBlock();
textBlock.VerticalAlignment = VerticalAlignment.Center;
stack.Children.Add(textBlock);
}

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

完整代码:

public class TreeViewWithIcons : TreeViewItem
{
ImageSource iconSource;
TextBlock textBlock;
Image icon; public TreeViewWithIcons()
{
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
Header = stack;
//Uncomment this code If you want to add an Image after the Node-HeaderText
//textBlock = new TextBlock();
//textBlock.VerticalAlignment = VerticalAlignment.Center;
//stack.Children.Add(textBlock);
icon = new Image();
icon.VerticalAlignment = VerticalAlignment.Center;
icon.Height = ;
icon.Width = ;
icon.Margin = new Thickness(, , , );
icon.Source = iconSource;
stack.Children.Add(icon);
//Add the HeaderText After Adding the icon
textBlock = new TextBlock();
textBlock.VerticalAlignment = VerticalAlignment.Center;
stack.Children.Add(textBlock);
} public ImageSource Icon
{
set
{
iconSource = value;
icon.Source = iconSource;
}
get
{
return iconSource;
}
} protected override void OnUnselected(RoutedEventArgs args)
{
base.OnUnselected(args);
icon.Source = iconSource;
} protected override void OnSelected(RoutedEventArgs args)
{
base.OnSelected(args);
icon.Source = iconSource;
} /// <summary>
/// Gets/Sets the HeaderText of TreeViewWithIcons
/// </summary>
public string HeaderText
{
set
{
textBlock.Text = value;
}
get
{
return textBlock.Text;
}
}
}

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. linux 打包和压缩的概念和区别

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名 给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rar. ...

  2. CenterOS7中解决No package mysql-server available.

    CenterOS7中解决No package mysql-server available. 1.使用yum install -y mysql-server报错如下: [root@heyong_jd ...

  3. 007-流程控制 if 语句

    流程控制 if 语句 if [ 条件判断式 ] ; then 程序 fi if [ 条件判断式 ] then 程序 fi 脚本示例: [root@zabbix lianxi]# .sh #!/bin/ ...

  4. Tymeleaf模板引擎背景图片路径书写方式

    <body style="background: url(../static/assets/img/bg-so-white.png);" th:style="'ba ...

  5. UI Recorder安装与使用

    现在的互联网公司,普遍在尝试并执行敏捷开发模式,那么必然要涉及到频繁的更新迭代,在每次更新迭代时,老功能的回归成为了老大难.当系统日益复杂,涉及到的回归点逐渐增多,UI自动化测试即使成本在大,也需要提 ...

  6. get和post请求方式的区别,常见状态码的整理

    get和post的区别 get和post是什么? HTTP协议中的两种发送请求的方法.get从指定的资源请求数据: post向指定的资源提交要被处理的数据. HTTP是什么? 超文本传输协议(HTTP ...

  7. php array_push()函数 语法

    php array_push()函数 语法 作用:向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度.博智达 语法:array_push(array,value1,value2.. ...

  8. java 如何实现大文件上传下载(传输)各种格式

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  9. 最小生成树(Kruskal算法)模板

    #include<iostream> #include<algorithm> using namespace std; ],n; struct node { int u,v,v ...

  10. sh_09_格式化输出

    sh_09_格式化输出 # 定义字符串变量 name,输出 我的名字叫 小明,请多多关照! name = "大小明" print("我的名字叫 %s,请多多关照!&quo ...