TreeView and wpftreeview with option boxes for wpf enterprise applications

Source: Internet
Author: User

TreeView and wpftreeview with option boxes for wpf enterprise applications

HierarchicalDataTemplate is used for hierarchical binding in wpf. Here we will talk about treeview with checkbox. For specific effects, see several common business scenarios in enterprise-level development of wpf.

Let's take a look at my control binding. Here I implement editing of module permissions. The specific effect is to select a node, and the status of the child node and the child node will change accordingly. The specific change logic is well understood and described.

<TreeView Name="TreeView_Right" ItemsSource="{Binding ModuleRight}">        <TreeView.ItemTemplate>            <HierarchicalDataTemplate DataType="{x:Type localModel:ModuleRight}" ItemsSource="{Binding ModuleChildren}">                <CheckBox IsThreeState="{Binding IsThreeState}" Content="{Binding MenuName}" IsChecked="{Binding IsChecked}" IsEnabled="{Binding IsEnabled}"/>            </HierarchicalDataTemplate>        </TreeView.ItemTemplate>        <TreeView.ItemContainerStyle>            <Style TargetType="TreeViewItem"  BasedOn="{StaticResource TreeViewItemStyle}">                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=OneWay}"/>            </Style>        </TreeView.ItemContainerStyle></TreeView>

The following are the corresponding binding classes for your reference only. Here, we need to note that IsChecked recursively modifies the IsChecked attribute of the parent node and child node each time it changes. Other attributes are no different from ordinary treeview bindings, if you have any questions, go to MSDN to view the use of treeview.

Public class ModuleRight: policymodelbase {private ModuleRight _ parent; public ModuleRight Parent {get {return _ parent;} set {_ parent = value; OnPropertyChanged ("Parent ");}} private string _ menuName; public string MenuName {get {if (deleetting. getValue ("language") = "en_us") return MenuName_EN; return _ menuName;} set {_ menuName = value ;}} public string MenuName_EN {get; set;} public String ClassName {get; set;} private List <ModuleRight> _ moduleChildren; public List <ModuleRight> ModuleChildren {get {return _ moduleChildren ?? (_ ModuleChildren = new List <ModuleRight> () ;}set {_ moduleChildren = value ;}} private int _ rightValue; public int RightValue {get {return _ rightValue ;} set {_ rightValue = value ;}} private bool _ isThreeState; public bool IsThreeState {get {return _ isThreeState;} set {_ isThreeState = value; onPropertyChanged ("IsThreeState");} private bool _ isEnabled = true; public bool IsEnabled {g Et {return _ isEnabled;} set {_ isEnabled = value; OnPropertyChanged ("IsEnabled") ;}} private bool? _ IsChecked = false; public bool? IsChecked {get {return _ isChecked;} set {SetIsChecked (value, true, true) ;}} public bool IsExpanded {get {return _ isChecked! = False ;}} public void SetIsChecked (bool? Value, bool updateChildren, bool updateParent) {_ isChecked = value; if (updateChildren & _ isChecked. hasValue) // sets the selected status of the child node {ModuleChildren. forEach (c => c. setIsChecked (_ isChecked, true, false);} if (updateParent & Parent! = Null & Parent. IsThreeState) // sets the selected state of the ancestor node {Parent. VerifyCheckState ();} OnPropertyChanged ("IsChecked");} public void VerifyCheckState () {bool? State = true; for (int I = 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);} public ModuleRight () {} public ModuleRight (string menuName, string menuName_en, string className) {this. menuName = menuName; this. menuName_EN = menuName_en; this. className = className ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.