采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx

上传人:b****1 文档编号:13273146 上传时间:2023-06-12 格式:DOCX 页数:13 大小:26.12KB
下载 相关 举报
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第1页
第1页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第2页
第2页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第3页
第3页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第4页
第4页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第5页
第5页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第6页
第6页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第7页
第7页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第8页
第8页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第9页
第9页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第10页
第10页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第11页
第11页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第12页
第12页 / 共13页
采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx

《采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx》由会员分享,可在线阅读,更多相关《采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx(13页珍藏版)》请在冰点文库上搜索。

采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树.docx

采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树

摘要:

采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树,

支持勾选。

勾选父节点,子节点回全部自动勾选;子节点部分勾选时,父节点半勾选;子节点全部勾选时,父节点勾选。

反之亦然。

HierarchicalDataTemplate是分层数据模板,通常用于tree,menu等层级控件。

HierarchicalDataTemplate的ItemsSource属性绑定下一级数据源。

Model为行政区数据实体类,通常访问数据库获取数据并构建对象。

ViewModel为界面的抽象模型,表示界面的数据和行为,是Model和View的桥梁。

view就是界面。

一、代码

1、Model

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespaceWpfHierarchicalTemplate

{

publicclassDistrict

{

publicintID{get;set;}

publicstringXzqhdm{get;set;}//行政区划代码

publicstringXzqhmc{get;set;}//行政区划名称

publicintLevel{get;set;}//级别,0全国,1省,2地市,3县,4,乡镇,5,村

publicIListChildren{get;set;}

publicDistrictParent{get;set;}

}

}

  2、ViewModel

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Collections.ObjectModel;

usingSystem.Windows.Media;

usingSystem.Windows.Media.Imaging;

namespaceWpfHierarchicalTemplate

{

publicclassDistrictNodeViewModel:

ModelCommon.NotifyObject

{

privatebool?

isSelected=false;

publicbool?

IsSelected

{

get{returnisSelected;}

set

{

isSelected=value;

RaisePropertyChanged("IsSelected");

}

}

privatebool?

isChecked=false;

publicbool?

IsChecked

{

get{returnisChecked;}

set

{

SetIsChecked(value);

}

}

privatevoidSetIsChecked(bool?

value)

{

if(value!

=isChecked)

{

isChecked=value;

RaisePropertyChanged("IsChecked");

}

if(this.Children.Count>0&&this.Children[0].isChecked!

=value)

{

//设置子节点勾选状态

foreach(variteminthis.Children)

{

if(value!

=null)

{

item.IsChecked=value;

}

}

}

if(this.parent!

=null)

{

if(this.Parent.Children.Count==this.Parent.Children.Count(item=>item.isChecked==value))

{

//同一级节点全部选中,则父节点选中。

反之亦然。

this.Parent.IsChecked=value;

}

elseif(this.Parent.Children.Count>this.Parent.Children.Count(item=>item.isChecked==value))

{

if(this.Parent.IsChecked!

=null)

{

this.Parent.IsChecked=null;

}

}

}

}

privatebool?

isExpand=false;

publicbool?

IsExpand

{

get{returnisExpand;}

set

{

isExpand=value;

RaisePropertyChanged("IsExpand");

}

}

privateBitmapImageimg;

publicBitmapImageImg

{

get{returnimg;}

set

{

img=value;

RaisePropertyChanged("Img");

}

}

privateObservableCollectionchildren=newObservableCollection();

publicObservableCollectionChildren

{

get{returnchildren;}

set

{

children=value;

RaisePropertyChanged("Children");

}

}

privateDistrictNodeViewModelparent;

publicDistrictNodeViewModelParent

{

get{returnparent;}

set

{

parent=value;

RaisePropertyChanged("Parent");

}

}

privateDistrictdistrict;

publicDistrictDistrict

{

get{returndistrict;}

set

{

district=value;

RaisePropertyChanged("District");

}

}

}

}

 

 

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Collections.ObjectModel;

namespaceWpfHierarchicalTemplate

{

publicclassDistrictMainViewModel:

ModelCommon.NotifyObject

{

privateObservableCollectionvmNodes;

publicObservableCollectionVmNodes

{

get{returnvmNodes;}

set

{

vmNodes=value;

RaisePropertyChanged("VmNodes");

}

}

publicDistrictMainViewModel()

{

this.VmNodes=newObservableCollection

{

LoadData()

};

}

publicDistrictNodeViewModelLoadData()

{

ObservableCollectionrootNodes=newObservableCollection();

Districtd00=newDistrict()

{

Xzqhmc="全国",

Parent=null

};

Districtd0=newDistrict()

{

Xzqhmc="河南",

Parent=d00

};

Districtd1=newDistrict()

{

Xzqhmc="北京",

Parent=d00

};

Districtd2=newDistrict()

{

Xzqhmc="山东",

Parent=d00

};

Districtd11=newDistrict()

{

Xzqhmc="海淀区",

Parent=d1

};

Districtd12=newDistrict()

{

Xzqhmc="石景山区",

Parent=d1

};

Districtd13=newDistrict()

{

Xzqhmc="朝阳区",

Parent=d1

};

Districtd01=newDistrict()

{

Xzqhmc="商丘",

Parent=d0

};

Districtd02=newDistrict()

{

Xzqhmc="郑州",

Parent=d0

};

Districtd03=newDistrict()

{

Xzqhmc="周口",

Parent=d0

};

d1.Children=newList{d11,d12,d13};

d0.Children=newList{d01,d02,d03};

d00.Children=newList{d1,d2,d0};

rootNodes.Add(d00);

DistrictNodeViewModeldnv=newDistrictNodeViewModel();

dnv.District=rootNodes[0];

SetDNV(dnv,rootNodes[0]);

returndnv;

}

privatevoidSetDNV(DistrictNodeViewModelvm,Districtroot)

{

if(root==null||root.Children==null||root.Children.Count==0)

{

return;

}

foreach(variteminroot.Children)

{

DistrictNodeViewModelvmNew=newDistrictNodeViewModel();

vmNew.District=item;

vmNew.Parent=vm;

vmNew.Img=newSystem.Windows.Media.Imaging.BitmapImage(newUri("/dog.jpg",UriKind.Relative));

vm.Children.Add(vmNew);

SetDNV(vmNew,item);

}

}

}

}

 

  3、主窗口

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows;

usingSystem.Windows.Controls;

usingSystem.Windows.Data;

usingSystem.Windows.Documents;

usingSystem.Windows.Input;

usingSystem.Windows.Media;

usingSystem.Windows.Media.Imaging;

usingSystem.Windows.Navigation;

usingSystem.Windows.Shapes;

namespaceWpfHierarchicalTemplate

{

///

///MainWindow.xaml的交互逻辑

///

publicpartialclassMainWindow:

Window

{

publicMainWindow()

{

InitializeComponent();

this.DataContext=newDistrictMainViewModel();

}

}

}

  

4、前台xaml

Class="WpfHierarchicalTemplate.MainWindow"

xmlns="

xmlns:

x="

Title="MainWindow"Height="350"Width="525">

Key="treeTemplate"ItemsSource="{BindingChildren}">

 

  二、效果

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 兵器核科学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2