WPF implements hierarchical binding primarily using Hierarchicaldatatemplate, which focuses on the TreeView with a checkbox, with specific results seen in several common business scenarios in WPF enterprise-level development.
Let's take a look at my control bindings, which I've implemented here is the editing of module permissions. The specific effect is to select a node, the descendant nodes, ancestor node state will change correspondingly, the specific change logic we all understand, described is very wordy.
<TreeViewName= "Treeview_right"ItemsSource= "{Binding moduleright}"> <TreeView. ItemTemplate> <hierarchicaldatatemplateDataType= "{x:type localmodel:moduleright}"ItemsSource= "{Binding Modulechildren}"> <CheckBoxisthreestate= "{Binding isthreestate}"Content= "{Binding MenuName}"IsChecked= "{Binding IsChecked}"isenabled= "{Binding isenabled}"/> </hierarchicaldatatemplate> </treeview.itemtemplate> <TreeView. Itemcontainerstyle> <StyleTargetType= "TreeViewItem"BasedOn= "{StaticResource Treeviewitemstyle}"><setter property= "isexpanded" value= "{Binding isexpanded, Mode=oneway}"/></Style> </Treeview.itemcontainerstyle></TreeView>
The following is the corresponding binding class, for informational purposes only. What needs to be explained here is that ischecked each time the change will be recursive to modify the parent node, the child node of the IsChecked property, the other with the normal TreeView binding is no different, if the reader is unclear, you can go to MSDN to see the use of the TreeView.
Public classmoduleright:notifymodelbase{PrivateModuleright _parent; Publicmoduleright Parent {Get { return_parent; } Set{_parent=value; OnPropertyChanged ("Parent"); } } Private string_menuname; Public stringMenuName {Get { if(Appsetting.getvalue ("language") =="en_US") returnmenuname_en; return_menuname; } Set{_menuname=value; } } Public stringMenuname_en {Get; Set; } Public stringClassName {Get; Set; } PrivateList<moduleright>_modulechildren; PublicList<moduleright>Modulechildren {Get { return_modulechildren?? (_modulechildren =NewList<moduleright>()); } Set{_modulechildren=value; } } Private int_rightvalue; Public intRightvalue {Get { return_rightvalue; } Set{_rightvalue=value; } } Private BOOL_isthreestate; Public BOOLIsthreestate {Get { return_isthreestate; } Set{_isthreestate=value; OnPropertyChanged ("isthreestate"); } } Private BOOL_isenabled =true; Public BOOLisenabled {Get { return_isenabled; } Set{_isenabled=value; OnPropertyChanged ("isenabled"); } } Private BOOL? _ischecked =false; Public BOOL?IsChecked {Get { return_ischecked; } Set{setischecked (value,true,true); } } Public BOOLisexpanded {Get { return_ischecked! =false; } } Public voidSetischecked (BOOL? ValueBOOLUpdatechildren,BOOLupdateparent) {_ischecked=value; if(Updatechildren && _ischecked.hasvalue)//to set the selected state of a descendant node{Modulechildren.foreach (c= C.setischecked (_ischecked,true,false)); } if(updateparent && Parent! =NULL&& parent.isthreestate)//set the selected state of the ancestor node{parent.verifycheckstate (); } onpropertychanged ("IsChecked"); } Public voidverifycheckstate () {BOOL? State =true; for(inti =0; i < Modulechildren.count; i++) { BOOL? Current =Modulechildren[i]. IsChecked; if(Current = =NULL) { state=NULL; Break; } Else { if(I < Modulechildren.count-1&& Modulechildren[i]. IsChecked! = modulechildren[i +1]. IsChecked) { state=NULL; Break; } } } if(state! =NULL) State= modulechildren[0]. IsChecked; Setischecked (state,false,true); } Publicmoduleright () {} PublicModuleright (stringMenuName,stringMenuname_en,stringclassName) { This. MenuName =MenuName; This. Menuname_en =menuname_en; This. ClassName =ClassName; } }
WPF Enterprise Apps with the TreeView of the Options Box