WPF中TreeView数据结构解析
XAML.CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace TreeViewBindingDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
List<NodeEntry> m_NodeEntrys;
List<NodeEntry> m_outputList;
public MainWindow()
{
InitializeComponent();
m_NodeEntrys = new List<NodeEntry>()
{
new NodeEntry { ID = , Name = "北京市", ParentID = },
new NodeEntry { ID = , Name = "中国" },
new NodeEntry { ID = , Name = "吉林省", ParentID = },
new NodeEntry { ID = , Name = "上海市", ParentID = },
new NodeEntry { ID = , Name = "海淀区", ParentID = },
new NodeEntry { ID = , Name = "朝阳区", ParentID = },
new NodeEntry { ID = , Name = "大兴区", ParentID = },
new NodeEntry { ID = , Name = "白山市", ParentID = },
new NodeEntry { ID = , Name = "长春市", ParentID = },
new NodeEntry { ID = , Name = "抚松县", ParentID = },
new NodeEntry { ID = , Name = "靖宇县", ParentID = },
new NodeEntry { ID = , Name = "靖宇县" }
};
m_outputList = Bind(m_NodeEntrys);
this.treeView1.ItemsSource = m_outputList;
this.treeView2.ItemsSource = m_outputList;
} private List<NodeEntry> Bind(List<NodeEntry> nodes)
{
List<NodeEntry> outputList=new List<NodeEntry>();
for (int i = ; i < nodes.Count; i++)
{
nodes[i].IsChecked = false;
if (nodes[i].ParentID == -)
{
outputList.Add(nodes[i]);
}
else
{
FindDownward(nodes,nodes[i].ParentID).NodeEntrys.Add(nodes[i]);
}
}
return outputList;
} private NodeEntry FindDownward(List<NodeEntry> nodes, int id)
{
if (nodes == null) return null;
for (int i = ; i < nodes.Count; i++)
{
if (nodes[i].ID == id)
{
return nodes[i];
}
}
return null;
} private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
m_NodeEntrys.Add(new NodeEntry { ID = , IsChecked = true, Name = "法国" });
m_outputList.Add(new NodeEntry { ID = , IsChecked = true, Name = "法国" });
//m_outputList = Bind(m_NodeEntrys);
NodeEntry node = new NodeEntry();
this.treeView1.ItemsSource = m_outputList;
this.treeView2.ItemsSource = null;
this.treeView2.ItemsSource = m_outputList;
}
catch (Exception ex)
{
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{ } //双向绑定改名,选择
private void treeView2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
NodeEntry item = (NodeEntry)this.treeView2.SelectedItem;
item.Name = "dido";
item.IsChecked = true;
MessageBox.Show(item.ID.ToString());
} }
}
实体类:
using System.Collections.Generic;
using System.ComponentModel; namespace TreeViewBindingDemo
{
public class NodeEntry : INotifyPropertyChanged
{
public NodeEntry()
{
this.NodeEntrys = new List<NodeEntry>();
this.ParentID = -;
this.IsChecked = true;
}
int id;
public int ID
{
get { return id; }
set { id = value; this.OnPropertyChanged("ID"); }
}
string name;
public string Name
{
get { return name; }
set { name = value; this.OnPropertyChanged("Name"); }
}
public int ParentID { get; set; }
bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set { isChecked = value; this.OnPropertyChanged("IsChecked"); }
}
List<NodeEntry> nodeEntrys;
public List<NodeEntry> NodeEntrys
{
get { return nodeEntrys; }
set
{
nodeEntrys = value;
this.OnPropertyChanged("NodeEntrys");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
} }
XAML代码:
<Window x:Class="TreeViewBindingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:model="clr-namespace:TreeViewBindingDemo"
Title="MainWindow" Height="" Width="">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:NodeEntry}" ItemsSource="{Binding NodeEntrys}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<CheckBox Focusable="False"
VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding ID}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="" Width="170*"/>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="" />
<ColumnDefinition Width="" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="" />
</Grid.RowDefinitions>
<GridSplitter Grid.Column="" Grid.RowSpan="" Name="gridSplitter1" HorizontalAlignment="Stretch" />
<Button Content="确定" Grid.Column="" Grid.Row="" Height="" HorizontalAlignment="right" Margin="0,10,0,0" Name="btnOK" Click="btnOK_Click" VerticalAlignment="Top" Width="" IsDefault="True" />
<Button Content="取消" Grid.Column="" Grid.Row="" Height="" HorizontalAlignment="right" Margin="0,10,10,0" Name="btnCancel" Click="btnCancel_Click" VerticalAlignment="Top" Width="" IsCancel="True" />
<TreeView Grid.ColumnSpan="" Name="treeView1" >
</TreeView>
<TreeView Grid.Column="" Grid.ColumnSpan="" Name="treeView2" SelectedItemChanged="treeView2_SelectedItemChanged">
</TreeView>
</Grid> </Window>
WPF中TreeView数据结构解析的更多相关文章
- WPF中TreeView.BringIntoView方法的替代方案
原文:WPF中TreeView.BringIntoView方法的替代方案 WPF中TreeView.BringIntoView方法的替代方案 周银辉 WPF中TreeView.BringIntoVie ...
- WPF中TreeView的使用
因为项目中需要用到TreeView控件,由于是第一次在WPF中用到,因此事先在网上搜了很多关于数据绑定的方法介绍,个人经过实际应用,觉得WPF中的HierarchicalDataTemplate定义模 ...
- WPF中TreeView控件的使用案例
WPF总体来说还是比较方便的,其中变化最大的主要是Listview和Treeview控件,而且TreeView似乎在WPF是一个备受指责的控件,很多人说他不好用.我这个demo主要是在wpf中使用Tr ...
- WPF中TreeView单击展开其子元素以及点击一个元素展开其他元素收起
TreeView单击展开其子元素: 在WPF的TreeView控件中,要想展开它的子元素,我们必须要鼠标左键点两下或者右键点一下,那么我们怎样实现左键点一下就使它展开呢? Xaml: <Grid ...
- WPF中TreeView控件SelectedItemChanged方法的MVVM绑定
问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...
- WPF中TreeView控件数据绑定和后台动态添加数据(二)
写在前面:在(一)中,介绍了TreeView控件MVVM模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...
- WPF中TreeView控件数据绑定和后台动态添加数据(一)
数据绑定: 更新内容:补充在MVVM模式上的TreeView控件数据绑定的代码. xaml代码: <TreeView Name="syntaxTree" ItemsSourc ...
- WPF中TreeView的+-号和连线style的一种实现
最近又开始跟WPF打交道,项目里面用到了TreeView这个控件.然后需要有一个连线的外观就像是这样 二话不说,百度了一下,找到一个实现, 通道. 把代码拷贝到项目里面,跑了一下,看上去还不错.但是这 ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
随机推荐
- leecode 排列的学习
前面写过3个排列.这里再写一次. 1.全部都不重复https://oj.leetcode.com/problems/permutations/ (使用交换法)只是本人对c++ stl不熟,不会把排列结 ...
- Spring框架整合Struts2
1,用Spring架构,及Struts2-spring-plugin插件 导入Spring的dist全部所需的jar包 Struts2的spring插件 struts2-spring-plugin.X ...
- 神经网络原理及其c++实现
1引言 数字识别是模式识别领域 中的一个重要分支,数字识别一般通过特征匹配及特征判别的传统方法进行处理.特征匹配通常适用于规范化的印刷体字符的识别,而 特征判别多用于手写字符识别,这些方法还处于探索阶 ...
- [Unix.C]Files and Directories
stat, fstat, and lstat Functions 本部分讨论的内容主要围绕3个stat函数及其返回值. #include <sys/stat.h> int stat(co ...
- c语音中打印参数调用层级即call stack, call trace
http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c There's backtrace(), a ...
- windows ntp安装及调试
Setting up NTP on Windows It's very helpful that Meinberg have provided an installer for the highly- ...
- 中文字符串的编码转换(c实现)
中文字符串在c/c++中表示为字节序列,在分词的时候需要根据不同的编码方式进行分词,一般分词器需要转换成统一的编码方式再进行转换,有些分词器如ICTCLAS在分词的时候可以不显示定义编码方式,可以检测 ...
- sql优化-hint的作用
目前,oracle采用的是CBR优化器,所以在有些时候,机器会按照自己的意愿去执行sql,当然oracle是根据本身的一些信息来做决定的,比如:统计信息.但有些时候,机器并不一定会按照我们预想的那样去 ...
- XMind十大最有用的功能
XMind十大最有用的功能 XMind是一款顶级商业品质的思维导图软件和头脑风暴软件,在企业和教育领域都有很广泛的应用,XMind功能全面,易上手,在此小编给大家整理出了XMind十大最有用的功能以供 ...
- 草稿-Hyper-V
Hyper-V Over SMB3.0 为Hyper-v宿主机和故障转移群集做防病毒排除 微软SMB 3.0文件共享协议新特性介绍