博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[WPF实用技巧]如何使WPF的TreeView节点之间有连线
阅读量:5035 次
发布时间:2019-06-12

本文共 2708 字,大约阅读时间需要 9 分钟。

 

 

 

 

 

 

 

示例代码:

原文地址:http://www.codeproject.com/Tips/673071/WPF-TreeView-with-WinForms-Style-Fomat

 

Introduction

WPF default TreeView is very good, but many people still want it to have lines join each of its child elements, like Windows Forms TreeView, including me. I have searched on the internet and have some examples, but they were not designed well enough.

Now, I myself designed a TreeView with style as WinForms. Hope this will help many people!

Source Code

All you need is an XAML file and a code behind.

First, you need draw Toggle Button: From Triangle button to Plus-Minus button: draw a rectangle with dark border, then draw two lines, one vertical line and one horizontal line. When TreeViewItem is expanded, the vertical line will hide:

In the above code, you can see a trigger, it will make the vertical line inside toggle button hide if item is expanded, or show if its children collapsed.

Then, you need to draw vertical and horizontal connecting lines between nodes: You need to redesignTreeViewItem control. Add these connecting lines:

to your TreeViewItem template like this:

Then, you need put the class TreeViewLineConverter to your namespace. This class will change the connecting lines if the item is the last in the list:

using System;using System.Windows;using System.Windows.Controls;using System.Windows.Data;namespace TreeViewEx{    public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } class TreeViewLineConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { TreeViewItem item = (TreeViewItem)value; ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item); return ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return false; } } }

Insert your namespace to your XAML, i.e.:

Add this line to Window.Resources:

Add trigger to TreeViewItem template, this trigger changes the connecting lines if the item is the last in the list:

The TreeView will have WinForms style now. You can add more trigger to control behavior of TreeView if you want. The full trigger can be found in the attached file.

ToDo

In WinForms TreeView, the connecting line is a dotted line. To make these lines dotted, change:

To:

But it is not pretty, as you see. As I'm a newbie in WPF, I don't know to style these lines perfectly.

Reference

This is the code I referenced before I wrote my own:

 

转载于:https://www.cnblogs.com/zhili/p/WPFTreeViewEx.html

你可能感兴趣的文章
山寨机与国产手机的历史
查看>>
FFMPEG转码后得到的MP4必须要加载完才能播放的问题
查看>>
刷过一题之NOIP2013表达式求值
查看>>
【HNOI2015】菜肴制作
查看>>
javascript继承
查看>>
海思uboot启动流程详细分析(三)【转】
查看>>
.NET之JSON序列化运用
查看>>
一般8位的微型机系统以16位来表示地址,则该计算机系统有几个地址空间
查看>>
sitemesh2.x+velocity+springmvc乱码解决方案
查看>>
(单例设计模式之一)饿汉式的反射与反序列化漏洞
查看>>
java中的final关键字和java抽象类
查看>>
用CSS3制作很特别的波浪形菜单
查看>>
Python面向对象03/继承
查看>>
Bing Maps进阶系列二:使用GeocodeService进行地理位置检索
查看>>
vector容器
查看>>
IDEA中使用mybatis逆向工程
查看>>
java序列化和反序列化
查看>>
绝对定位
查看>>
ubuntu集群下ssh配置总结
查看>>
flink源码编译(windows环境)
查看>>