TreeView —WPF—MVVM—HierarchicalDataTemplate

來源:互聯網
上載者:User

標籤:pac   documents   grid   sha   exp   str   pes   raise   mon   

摘要:採用HierarchicalDataTemplate資料範本和treeview在MVVM模式下實現行政區劃樹,

支援勾選。

一、代碼

1、Model

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace WpfHierarchicalTemplate{    public class District    {        public int ID { get; set; }        public string   Xzqhdm { get; set; }//行政區劃代碼        public string Xzqhmc { get; set; }//行政區劃名稱        public int Level { get; set; }//層級,0全國,1省,2地市,3縣,4,鄉鎮,5,村        public IList<District> Children { get; set; }        public District Parent { get; set; }    }}

  2、ViewModel

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections.ObjectModel;namespace WpfHierarchicalTemplate{    public class DistrictNodeViewModel : ModelCommon.NotifyObject    {        private bool? isSelected = false;        public bool? IsSelected        {            get { return isSelected; }            set            {                isSelected = value;                RaisePropertyChanged("IsSelected");            }        }        private bool? isChecked = false;        public bool? IsChecked        {            get { return isChecked; }            set            {                if (value != isChecked)                {                    isChecked = value;                    RaisePropertyChanged("IsChecked");                }                if (this.Children.Count > 0&&this.Children[0].isChecked!=value)                {                    foreach (var item in this.Children)                    {                        item.IsChecked = value;                    }                }                if (this.parent!=null&&this.Parent.Children.Count==this.Parent.Children.Count(item=>item.isChecked==value))                {                    this.Parent.IsChecked = value;                    return;                }            }        }        private bool? isExpand = false;        public bool? IsExpand        {            get { return isExpand; }            set            {                isExpand = value;                RaisePropertyChanged("IsExpand");            }        }        private ObservableCollection<DistrictNodeViewModel> children = new ObservableCollection<DistrictNodeViewModel>();        public ObservableCollection<DistrictNodeViewModel> Children        {            get { return children; }            set            {                children = value;                RaisePropertyChanged("Children");            }        }        private DistrictNodeViewModel parent;        public DistrictNodeViewModel Parent        {            get { return parent; }            set            {                parent = value;                RaisePropertyChanged("Parent");            }        }        private District district;        public District District        {            get { return district; }            set            {                district = value;                RaisePropertyChanged("District");            }        }    }}

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections.ObjectModel;namespace WpfHierarchicalTemplate{    public class DistrictMainViewModel : ModelCommon.NotifyObject    {        private ObservableCollection<DistrictNodeViewModel> vmNodes;        public ObservableCollection<DistrictNodeViewModel> VmNodes        {            get { return vmNodes; }            set            {                vmNodes = value;                RaisePropertyChanged("VmNodes");            }        }        public DistrictMainViewModel()        {            this.VmNodes = new ObservableCollection<DistrictNodeViewModel>            {                LoadData()            };        }        public DistrictNodeViewModel LoadData()        {            ObservableCollection<District> rootNodes =new ObservableCollection<District>();            District d00 = new District()            {                Xzqhmc = "全國",                Parent = null            };            District d0 = new District()            {                 Xzqhmc="河南",                   Parent=d00                              };            District d1 = new District()            {                Xzqhmc = "北京",                Parent = d00            };            District d2 = new District()            {                Xzqhmc = "山東",                Parent = d00            };            District d11 = new District()            {                Xzqhmc = "海澱區",                Parent = d1            };            District d12 = new District()            {                Xzqhmc = "石景山區",                Parent = d1            };            District d13 = new District()            {                Xzqhmc = "朝陽區",                Parent = d1            };                        District d01 = new District()            {                Xzqhmc = "商丘",                Parent = d0            };            District d02 = new District()            {                Xzqhmc = "鄭州",                Parent = d0            };            District d03 = new District()            {                Xzqhmc = "周口",                Parent = d0            };            d1.Children = new List<District> { d11, d12, d13 };            d0.Children = new List<District> { d01, d02, d03 };            d00.Children = new List<District>{d1,d2,d0};            rootNodes.Add(d00);            DistrictNodeViewModel dnv = new DistrictNodeViewModel();            dnv.District = rootNodes[0];            SetDNV(dnv, rootNodes[0]);            return dnv;        }        private void SetDNV(DistrictNodeViewModel vm,District root)        {            if (root==null||root.Children==null||root.Children.Count==0)            {                return;            }            foreach (var item in root.Children)            {                DistrictNodeViewModel vmNew = new DistrictNodeViewModel();                vmNew.District = item;                vmNew.Parent = vm;                vm.Children.Add(vmNew);                SetDNV(vmNew, item);            }        }    }}

  3、主視窗

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 WpfHierarchicalTemplate{    /// <summary>    /// MainWindow.xaml 的互動邏輯    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            this.DataContext = new DistrictMainViewModel();        }    }}

  

4、前台xaml

<Window x:Class="WpfHierarchicalTemplate.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525">    <Window.Resources>        <HierarchicalDataTemplate x:Key="treeTemplate" ItemsSource="{Binding Children}">            <StackPanel Orientation="Horizontal">                <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>                <TextBlock Text="{Binding District.Xzqhmc}"></TextBlock>            </StackPanel>        </HierarchicalDataTemplate>    </Window.Resources>    <Grid>        <TreeView  ItemTemplate="{StaticResource treeTemplate}" ItemsSource="{Binding VmNodes}">        </TreeView>    </Grid></Window>

  二、效果

TreeView —WPF—MVVM—HierarchicalDataTemplate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.