-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTreeItem.cs
124 lines (105 loc) · 3.36 KB
/
TreeItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2013, 2020 Dijji, and released under Ms-PL. This, with other relevant licenses, can be found in the root of this distribution.
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;
namespace CustomWindowsProperties
{
public class TreeItem : INotifyPropertyChanged
{
string name = null;
bool isSelected = false;
TreeItem parent = null;
readonly ObservableCollection<TreeItem> children = new ObservableCollection<TreeItem>();
public event NameChangedEventHandler NameChanged;
public TreeItem(string name, object item = null)
{
this.name = name;
this.Item = item;
}
public string Name { get { return name; } }
public object Item { get; set; }
public string Tag { get; set; }
public TreeItem Parent { get { return parent; } }
public ObservableCollection<TreeItem> Children { get { return children; } }
public Brush Background { get { return background; } set { background = value; OnPropertyChanged(); } }
private Brush background = null;
public bool IsSelected
{
get
{
return isSelected;
}
set
{
if (value != isSelected)
{
isSelected = value;
OnPropertyChanged();
}
}
}
public bool IsExpanded { get { return isExpanded; } set { isExpanded = value; OnPropertyChanged(); } }
private bool isExpanded;
public string Path
{
get
{
if (Parent == null)
return Name;
else
return Parent.Path + "." + Name;
}
}
public string EditableName
{
get
{
return Name;
}
set
{
NameChanged?.Invoke(this, new NameChangedEventArgs(value));
}
}
public void AddChild(TreeItem child)
{
child.parent = this;
Children.Add(child);
}
public void InsertChild(int index, TreeItem child)
{
child.parent = this;
Children.Insert(index, child);
}
public void RemoveChild(TreeItem child)
{
child.parent = null;
Children.Remove(child);
}
public void AbandonNameChange()
{
OnPropertyChanged(nameof(EditableName));
}
public void ChangeName(string newName)
{
name = newName;
OnPropertyChanged(nameof(Name));
OnPropertyChanged(nameof(EditableName));
}
public TreeItem Clone()
{
TreeItem clone = new TreeItem(this.Name, this.Item);
foreach (var ti in this.Children)
clone.AddChild(ti.Clone());
return clone;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion
}
}