How to develop treeview with checkboxes in wpf?

后端 未结 5 1062
小蘑菇
小蘑菇 2020-12-16 21:41

I have a requirement that , I need to add nodes to a TreeView dynamically and that nodes with CheckBoxes. If one CheckBox is selected

5条回答
  •  自闭症患者
    2020-12-16 22:40

    Check this out:

    enter image description here

    DataModel.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace WpfApplication102
    {
        public class Family : DependencyObject
        {
            public string Name { get; set; }
            public List Members { get; set; }
        }
    
        public class Person : DependencyObject
        {
            public string Name { get; set; }
        }
    }
    

    ItemHelper.cs

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace WpfApplication102
    {
        public class ItemHelper : DependencyObject
        {
            public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.RegisterAttached("IsChecked", typeof(bool?), typeof(ItemHelper), new PropertyMetadata(false, new PropertyChangedCallback(OnIsCheckedPropertyChanged)));
            private static void OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if (d is Family && ((bool?)e.NewValue).HasValue)
                    foreach (Person p in (d as Family).Members)
                        ItemHelper.SetIsChecked(p, (bool?)e.NewValue);
    
                if (d is Person)
                {
                    int checked = ((d as Person).GetValue(ItemHelper.ParentProperty) as Family).Members.Where(x => ItemHelper.GetIsChecked(x) == true).Count();
                    int unchecked = ((d as Person).GetValue(ItemHelper.ParentProperty) as Family).Members.Where(x => ItemHelper.GetIsChecked(x) == false).Count();
                    if (unchecked > 0 && checked > 0)
                    {
                        ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, null);
                        return;
                    }
                    if (checked > 0)
                    {
                        ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, true);
                        return;
                    }
                    ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, false);
                }
            }
            public static void SetIsChecked(DependencyObject element, bool? IsChecked)
            {
                element.SetValue(ItemHelper.IsCheckedProperty, IsChecked);
            }
            public static bool? GetIsChecked(DependencyObject element)
            {
                return (bool?)element.GetValue(ItemHelper.IsCheckedProperty);
            }
    
            public static readonly DependencyProperty ParentProperty = DependencyProperty.RegisterAttached("Parent", typeof(object), typeof(ItemHelper));
            public static void SetParent(DependencyObject element, object Parent)
            {
                element.SetValue(ItemHelper.ParentProperty, Parent);
            }
            public static object GetParent(DependencyObject element)
            {
                return (object)element.GetValue(ItemHelper.ParentProperty);
            }
        }
    }
    

    MainWindow.xaml

    
    
        
    
            
                
                    
                        
                            
                                
                            
                        
                    
                    
                        
                            
                                
                            
                        
                    
                
                
                    
                
            
    
            

    MainWindow.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    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 WpfApplication102
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        public partial class MainWindow : Window
        {
            public ObservableCollection Families { get; set; }
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.Families = new ObservableCollection();
                this.Families.Add(new Family() { Name = "Simpsons", Members = new List() { new Person() { Name = "Homer" }, new Person() { Name = "Bart" } } });
                this.Families.Add(new Family() { Name = "Griffin", Members = new List() { new Person() { Name = "Peter" }, new Person() { Name = "Stewie" } } });
                this.Families.Add(new Family() { Name = "Fry", Members = new List() { new Person() { Name = "Philip J." } } });
    
                foreach (Family family in this.Families)
                    foreach (Person person in family.Members)
                        person.SetValue(ItemHelper.ParentProperty, family);
            }
    
            private void Button_PrintCrew_Click(object sender, RoutedEventArgs e)
            {
                string crew = "";
                foreach (Family family in this.Families)
                    foreach (Person person in family.Members)
                        if (ItemHelper.GetIsChecked(person) == true)
                            crew += person.Name + ", ";
                crew = crew.TrimEnd(new char[] { ',', ' ' });
                this.textBoxCrew.Text = "Your crew: " + crew;
            }
        }
    }
    

提交回复
热议问题