c# WPF——创建带有图标的TreeView
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的更多相关文章
- WPF 带CheckBox、图标的TreeView
WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提 ...
- WPF 带CheckBox、图标的TreeView(转)
在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提供强大的ItemTemplate模板功能和自定义样式 ...
- 带有图标的MessageBox
MessageBoxUtils类的代码如下: Ext.define('org.pine.util.MessageBoxUtils', { singleton: true, /** 普通信息提示框 */ ...
- 使用StarUML创建类图
使用StarUML创建类图 http://www.flyne.org/article/379 1.综述(What) StarUML是一种生成类图和其他类型的UML图表的工具.本文是一个使用StarUM ...
- 利用WPF创建含多种交互特性的无边框窗体
咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...
- 如何用Rational rose创建类图
UML中各种图形概要: 图名 对照 说明 用例图 use case diagram 用例图表明系统做什么,与谁交互.用例是系统提供的功能,参与者是系统与谁交互,参与者可以是人.系统或其他实体.一个系统 ...
- 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据
原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...
- Qt 怎样生成带图标的exe
一.问题描述 当我们在 Windows 下用 VS 生成 exe 程序时,如果窗口程序指定了图标,那么生成的 exe 程序便是指定的图标模样. 但是,当使用 Qt Creator 编译程序却不同.即使 ...
- 【百度地图API】如何快速创建带有标注的地图?——快速创建地图工具+如何标注商家
原文:[百度地图API]如何快速创建带有标注的地图?--快速创建地图工具+如何标注商家 摘要: 如果你不会程序,如果你不想写代码. 如果你想拥有一张自己的地图,如果你想在该地图上标注出你商店的位置. ...
随机推荐
- React -- 3/100 】组件通讯
通讯 | props | prop-types 组件通讯 Props: 组件无论是使用函数声明还是通过 class 声明,都决不能修改自身的 props /* class */ .parent-box ...
- Site error: the ionCube PHP Loader needs to be installed.解决办法
问题描述: 有些模块的作者为了保护代码而采用ionCube加密的代码,所以这里必须给服务器装上这个php的扩展,就好像以前的zend一样 解决办法: http://bbs.52jscn.com/thr ...
- <img> 标签的 src 属性
src属性 加载的时候就会请求 1.servlet生成一个图片 2.你直接输入servlet的连接看一下,就是一个图片,和我们自己发布到服务器的一样. 3.页面加载时,会访问这个servelt连接,自 ...
- 08java进阶——异常
1.异常的概念 package cn.jxufe.java.chapter08.demo01; public class Test01ArithmeticException { public stat ...
- 六、ARM 寻址方式
寻址方式是针对源操作数来说的 6.1 立即数寻址 源操作数是立即数 立即数:操作码以 # 号开头的数字为立即数 立即数寻址: MOV R0, #0x300 伪指令: LDR R0 ...
- RouterOS基础安装配置
安装 光盘安装 载入光盘,按"A"全选按"I"进行安装,然后提示不否用旧的配置,按"N"全新安装,警告时按"Y"确定 安 ...
- 微信小程序细节部分
微信小程序和HTML区别: 1.开发工具不同,H5的开发工具+浏览器Device Mode预览,小程序的开发基于自己的开发者工具 2.开发语言不同,小程序自己开发了一套WXML标签语言和WXSS样式语 ...
- dnsmasq+Stunnel+sniproxy加密代理
一.环境介绍 [root@kimissVPN ~]# cat /etc/redhat-release CentOS release 6.8 (Final) [root@kimissVPN ~]# un ...
- SQL 介绍和操作
1.什么是SQL SQL的全称是“结构话查询语句”(Structured Query Language ),是1974年有Boyce和chamberlin 提出来的.经过多年的发展,SQL语言已经成为 ...
- Linux技术学习要点,您掌握了吗---初学者必看
1.如何做好嵌入式Linux学习前的准备? 要成为一名合格的嵌入式Linux工程师,就需要系统的学习软.硬件相关领域内的知识,需要在最开始就掌握开发的规范和原则,养成良好的工作习惯.为了确保学习的效果 ...