wpf Checked-TreeView עם Binding
-
מצו"ב קוד לפרוייקט שלי עבור יצירת
wpf Checked-TreeView עם Binding
אשמח לקבל משוב. תודה
כמו"כ לא הצלחתי לעשות BInding ל itemsorce ישירות מה-xaml רק דרך הקוד זה עובד - אשמח לקבל הדרכה בנושא ממי שיכול.
תודה מראש.
עשיתי את זה בעיקר ללימוד - מקווה שיהיה שימושי בעז"ה לעוד מישהו חוץ ממני.<Window x:Class="Wpf_Checked_TreeView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Wpf_Checked_TreeView" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Self}}" > <Grid> <TreeView x:Name="treeView" ItemsSource="{Binding treeItemsList}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:CheckedTreeItem}" ItemsSource="{Binding Children}"> <StackPanel Orientation="Horizontal"> <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked, Mode=TwoWay}"/> </StackPanel> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> </Grid> </Window>
using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; namespace Wpf_Checked_TreeView { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public ObservableCollection<object> treeItemsList = new ObservableCollection<object>(); public MainWindow() { InitializeComponent(); CheckedTreeItem rootItem1 = new CheckedTreeItem { Name = "root item 1" }; CheckedTreeItem child1 = new CheckedTreeItem { Name = "child item 1"}; CheckedTreeItem child2 = new CheckedTreeItem { Name = "child item 2" }; rootItem1.AddChild(child1); rootItem1.AddChild(child2); child1.AddChild(new CheckedTreeItem { Name = "sub child item 1" }); child1.AddChild(new CheckedTreeItem { Name = "sub child item 2" }); child2.AddChild(new CheckedTreeItem { Name = "sub child item 1" }); child2.AddChild(new CheckedTreeItem { Name = "sub child item 2" }); CheckedTreeItem rootItem2 = new CheckedTreeItem { Name = "root item 1" }; rootItem2.AddChild(new CheckedTreeItem { Name = "child item 1" }); rootItem2.AddChild(new CheckedTreeItem { Name = "child item 1" }); treeItemsList.Add(rootItem1); treeItemsList.Add(rootItem2); treeView.ItemsSource = treeItemsList; } } public class CheckedTreeItem : INotifyPropertyChanged { private ObservableCollection<CheckedTreeItem> _children = new ObservableCollection<CheckedTreeItem>(); public ObservableCollection<CheckedTreeItem> Children { get { return _children; } set { _children = value; OnPropertyChanged(nameof(Children)); } } private CheckedTreeItem _parent; public CheckedTreeItem Parent { get { return _parent; } set { _parent = value; OnPropertyChanged(nameof(Parent)); } } private string _name; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged(nameof(Name)); } } } private bool? _isChecked = false; public bool? IsChecked { get { return _isChecked; } set { if (_isChecked != value) { _isChecked = value; OnPropertyChanged(nameof(IsChecked)); UpdateChildCheckSatus(value); UpdateParentCheckSatus(value); } } } void UpdateChildCheckSatus(bool? value) { foreach (CheckedTreeItem child in Children) { if (value != null){ child.IsChecked = value; } } } void UpdateParentCheckSatus(bool? value) { if (Parent != null) { bool allChecked = Parent.Children.OfType<CheckedTreeItem>().All(child => child.IsChecked == true); bool allUnchecked = Parent.Children.OfType<CheckedTreeItem>().All(child => child.IsChecked == false); if (allChecked) { Parent.IsChecked = true; } else if (allUnchecked) { Parent.IsChecked = false; } else { Parent.IsChecked = null; } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public void AddChild(CheckedTreeItem child) { if (child is CheckedTreeItem CheckedTreeItem) { CheckedTreeItem.Parent = this; Children.Add(child); } } } }