WPF MVVM实现TreeView

简介:

今天有点时间,做个小例子WPF MVVM 实现TreeView 只是一个思路大家可以自由扩展

文章最后给出了源码下载地址

图1   图2     

模版加上了一个checkbox,选中父类的checkbox 所有的子类也相就选中。

如果子类没有全部父类的checkbox不会选中

用vmmm我们要先实现INotifyPropertyChanged

/// <summary>
///
/// </summary>
public  class  NotifyPropertyBase : INotifyPropertyChanged
{
     #region INotifyPropertyChanged
     public  void  OnPropertyChanged( string  propertyName)
     {
         if  (PropertyChanged != null )
         {
             PropertyChanged( this , new  PropertyChangedEventArgs(propertyName));
         }
     }
     public  event  PropertyChangedEventHandler PropertyChanged;
     #endregion
}

为了避免硬编码错误我写一个扩展方法

/// <summary>
   /// 扩展方法
   /// 避免硬编码问题
   /// </summary>
   public  static  class  NotifyPropertyBaseEx
   {
       public  static  void  SetProperty<T, U>( this  T tvm, Expression<Func<T, U>> expre) where  T : NotifyPropertyBase, new ()
       {
           string  _pro = CommonFun.GetPropertyName(expre);
           tvm.OnPropertyChanged(_pro);
       }
   }
   #endregion
 
 
   public  class  CommonFun
   {
       /// <summary>
       /// 返回属性名
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <typeparam name="U"></typeparam>
       /// <param name="expr"></param>
       /// <returns></returns>
       public  static  string  GetPropertyName<T, U>(Expression<Func<T, U>> expr)
       {
           string  _propertyName = "" ;
           if  (expr.Body is  MemberExpression)
           {
               _propertyName = (expr.Body as  MemberExpression).Member.Name;
           }
           else  if  (expr.Body is  UnaryExpression)
           {
               _propertyName = ((expr.Body as  UnaryExpression).Operand as  MemberExpression).Member.Name;
           }
           return  _propertyName;
       }
   }

下面我们就来实现treeveivew的绑定类

/// <summary>
    /// 因为用到泛型了不能写成abstract 类
    ///
    /// </summary>
    public   class  MyTree : NotifyPropertyBase
    {
 
 
        #region 父
        public  MyTree Parent
        {
            get ;
            set ;
        }
        #endregion
 
        #region 子
        public  List<MyTree> Children
        {
            get ;
            set ;
        }
        #endregion
 
        #region 节点的名字
        public  string  Name
        {
            get ;
            set ;
        }
        #endregion
 
        #region Constructors
        public  MyTree( string  name)
        {
            this .Name=name;
            this .Children= new  List<MyTree>();
        }
        public  MyTree() { }
 
        public
        #endregion
 
        #region CheckBox是否选中
        bool ? _isChecked;
        public  bool ? IsChecked
        {
            get
            {
                return  _isChecked;
            }
            set
            {
                SetIsChecked(value, true , true );
            }
        }
 
        private  void  SetIsChecked( bool ? value, bool  checkedChildren, bool  checkedParent)
        {
            if  (_isChecked == value) return ;
            _isChecked = value;
            //选中和取消子类
            if  (checkedChildren && value.HasValue && Children != null )
                Children.ForEach(ch => ch.SetIsChecked(value, true , false ));
 
            //选中和取消父类
            if  (checkedParent && this .Parent != null )
                this .Parent.CheckParentCheckState();
 
            //通知更改
            
            this .SetProperty(x => x.IsChecked);
        }
 
        /// <summary>
        /// 检查父类是否选 中
        /// 如果父类的子类中有一个和第一个子类的状态不一样父类ischecked为null
        /// </summary>
        private  void  CheckParentCheckState()
        {
            bool ? _currentState = this .IsChecked;
            bool ? _firstState = null ;
            for  ( int  i = 0; i < this .Children.Count(); i++)
            {
                bool ? childrenState = this .Children[i].IsChecked;
                if  (i == 0)
                {
                    _firstState = childrenState;
                }
                else  if  (_firstState != childrenState)
                {
                    _firstState = null ;
                }
            }
            if  (_firstState != null ) _currentState = _firstState;
            SetIsChecked(_firstState, false , true );
        }
 
        #endregion
 
        #region 选中的行 IsSelected
        bool  _isSelected;
        public  bool  IsSelected
        {
            get
            {
                return  _isSelected;
            }
            set
            {
                _isSelected = value;
                this .SetProperty(x => x.IsChecked);
                if  (_isSelected)
                {
                    SelectedTreeItem = this ;
                    MessageBox.Show( "选中的是"  + SelectedTreeItem.Name);
                }
                else
                    SelectedTreeItem = null ;
            }
        }
        #endregion
 
        #region 选中的数据
        public  MyTree SelectedTreeItem
        {
            get ;
            set ;
        }
        #endregion
 
        #region 创建树
 
        public  void  CreateTreeWithChildre( MyTree children, bool ? isChecked)
        {
            this .Children.Add(children);
 
            children.Parent = this ;
            children.IsChecked = isChecked;
        }
        #endregion
    }

 

我们再下面实现ViewModel

public  class  TreeViewModel:NotifyPropertyBase
    {
        public  List<MyTree> MyTrees
        {
            get ;
            set ;
        }
        public  TreeViewModel()
        {
            MyTrees = new  List<MyTree>();
            MyTrees.Add(MyCreateTree());
            
        }
        /// <summary>
        /// 创建树
        /// </summary>
        /// <returns></returns>
        public  MyTree MyCreateTree()
        {
            MyTree _myT = new  MyTree( "中国" );
            #region 北京
            MyTree _myBJ = new  MyTree( "北京" );
            _myT.CreateTreeWithChildre(_myBJ, false );
            MyTree _HD = new  MyTree( "海淀区" );
            
            
            MyTree _CY = new  MyTree( "朝阳区" );
            MyTree _FT = new  MyTree( "丰台区" );
            MyTree _DC = new  MyTree( "东城区" );
 
            _myBJ.CreateTreeWithChildre(_HD, false );
            _HD.CreateTreeWithChildre( new  MyTree( "某某1" ), false );
            _HD.CreateTreeWithChildre( new  MyTree( "某某2" ), true );
            _myBJ.CreateTreeWithChildre(_CY, false );
            _myBJ.CreateTreeWithChildre(_FT, false );
            _myBJ.CreateTreeWithChildre(_DC, false );
 
            #endregion
 
            #region 河北
            MyTree _myHB = new  MyTree( "河北" );
            _myT.CreateTreeWithChildre(_myHB, false );
            MyTree _mySJZ = new  MyTree( "石家庄" );
            MyTree _mySD = new  MyTree( "山东" );
         
            MyTree _myTS = new  MyTree( "唐山" );
 
            _myHB.CreateTreeWithChildre(_mySJZ, true );
            _myHB.CreateTreeWithChildre(_mySD, false );
            _myHB.CreateTreeWithChildre(_myTS, false );
            #endregion
 
            return  _myT;
        }
 
        
    }

  

我们再实现一个TreeView的模版

复制代码
<Window x:Class="MyWpfCheckTreeDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:VM="clr-namespace:MyWpfCheckTreeDemo.AppViewModel"
        Title="MainWindow" Height="350" Width="525" Loaded="LoadedEvent">
    <Window.Resources>
        <HierarchicalDataTemplate x:Key="MyTreeItemTemplate"  DataType="{x:Type VM:MyTree}" ItemsSource="{Binding Path=Children,Mode=OneWay}">
            <StackPanel x:Name="My_SP"  Orientation="Horizontal" Margin="2">
                <CheckBox  IsChecked="{Binding Path=IsChecked}" >
                </CheckBox>
                <ContentPresenter  Content="{Binding Path=Name,Mode=OneTime}" Margin="2,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="True" />
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected,Mode=TwoWay}"/>
        </Style>
    </Window.Resources>
    <Grid>
        <DockPanel>
            
            <TreeView  x:Name="tv" ItemsSource="{Binding MyTrees}"
                      ItemContainerStyle="{StaticResource TreeViewItemStyle}"
                      ItemTemplate="{StaticResource MyTreeItemTemplate}"
                      ></TreeView>
        </DockPanel>
        
    </Grid>
</Window> 
复制代码

 

 源码地址:MyWpfCheckTreeDemo.rar

 本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/p/3152982.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
66
分享
相关文章
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
486 0
WPF开发者必读:MVVM模式实战,轻松构建可维护的应用程序,让你的代码更上一层楼!
【8月更文挑战第31天】在WPF应用程序开发中,MVVM(Model-View-ViewModel)模式通过分离关注点,提高了代码的可维护性和可扩展性。本文详细介绍了MVVM模式的三个核心组件:Model(数据模型)、View(用户界面)和ViewModel(处理数据绑定与逻辑),并通过示例代码展示了如何在WPF项目中实现MVVM模式。通过这种模式,开发者可以更高效地构建桌面应用程序。希望本文能帮助你在WPF开发中更好地应用MVVM模式。
374 1
WPF 项目中 MVVM模式 的简单例子说明
本文通过WPF项目中的加法操作示例,讲解了MVVM模式的结构和实现方法,包括数据模型、视图、视图模型的创建和数据绑定,以及命令的实现和事件通知机制。
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(5) -- 树列表TreeView的使用
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(5) -- 树列表TreeView的使用
“深度剖析WPF开发中的设计模式应用:以MVVM为核心,手把手教你重构代码结构,实现软件工程的最佳实践与高效协作”
【8月更文挑战第31天】设计模式是在软件工程中解决常见问题的成熟方案。在WPF开发中,合理应用如MVC、MVVM及工厂模式等能显著提升代码质量和可维护性。本文通过具体案例,详细解析了这些模式的实际应用,特别是MVVM模式如何通过分离UI逻辑与业务逻辑,实现视图与模型的松耦合,从而优化代码结构并提高开发效率。通过示例代码展示了从模型定义、视图模型管理到视图展示的全过程,帮助读者更好地理解并应用这些模式。
204 0
WPF开发者必读:MVVM模式实战,轻松实现现代桌面应用架构,让你的代码更上一层楼!
【8月更文挑战第31天】在WPF应用程序开发中,MVVM(Model-View-ViewModel)模式通过分离应用程序的逻辑和界面,提高了代码的可维护性和可扩展性。本文介绍了MVVM模式的三个核心组件:Model(数据模型)、View(用户界面)和ViewModel(处理数据绑定和逻辑),并通过示例代码展示了如何在WPF项目中实现MVVM模式。通过这种方式,开发者可以构建更加高效和可扩展的桌面应用程序。
355 0
WPF/C#:理解与实现WPF中的MVVM模式
WPF/C#:理解与实现WPF中的MVVM模式
419 0
深入理解WPF中MVVM的设计思想
近些年来,随着WPF在生产,制造,工业控制等领域应用越来越广发,很多企业对WPF开发的需求也逐渐增多,使得很多人看到潜在机会,不断从Web,WinForm开发转向了WPF开发,但是WPF开发也有很多新的概念及设计思想,如:数据驱动,数据绑定,依赖属性,命令,控件模板,数据模板,MVVM等,与传统WinForm,ASP.NET WebForm开发,有很大的差异,今天就以一个简单的小例子,简述WPF开发中MVVM设计思想及应用。
141 0
WPF技术之TreeView控件
WPF TreeView控件是一个用于显示分层数据的控件,类似于文件浏览器中的目录结构。
225 0
WPF-Binding问题-MVVM中IsChecked属性CommandParameter转换值类型空异常
WPF-Binding问题-MVVM中IsChecked属性CommandParameter转换值类型空异常
227 0

相关课程

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等