Created
August 13, 2021 15:43
-
-
Save SmashBrando/8081672ece7eedca705ef21ccb64393f to your computer and use it in GitHub Desktop.
CheckedTreeViewItem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Controls; | |
public class CheckedTreeViewItem : ItemsControl, INotifyPropertyChanged | |
{ | |
bool? _isChecked = false; | |
string _imageSource = String.Empty; | |
int _imageIndex = -1; | |
public TreeView TreeView | |
{ | |
get | |
{ | |
if (this.Parent != null && this.Parent is TreeView) | |
{ | |
return this.Parent as TreeView; | |
} | |
else if (this.Parent != null && this.Parent is CheckedTreeViewItem) | |
{ | |
return ((CheckedTreeViewItem)this.Parent).TreeView; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
} | |
public string ImageSource | |
{ | |
get { return _imageSource; } | |
private set { _imageSource = value;} | |
} | |
public bool? IsChecked | |
{ | |
get { return _isChecked; } | |
set { this.SetIsChecked(value, true, true); } | |
} | |
public bool IsExpanded | |
{ | |
get; | |
set; | |
} | |
public string Text { get; set; } | |
public string Key { get; set; } | |
public int ImageIndex | |
{ | |
get | |
{ | |
return _imageIndex; | |
} | |
set | |
{ | |
_imageIndex = value; | |
if (_imageIndex >= 0) | |
{ | |
var imageList = UpgradeHelpers.VB6.WPF.ImageList.ImageListAttachedProperties.GetImageList(TreeView); | |
var extractedSource = ((Image)imageList.Items[_imageIndex]).Source; | |
_imageSource = extractedSource.ToString(); | |
} | |
else | |
{ | |
_imageSource = String.Empty; | |
} | |
} | |
} | |
public bool IsInitiallySelected { get; private set; } | |
public event PropertyChangedEventHandler PropertyChanged; | |
void SetIsChecked(bool? value, bool updateChildren, bool updateParent) | |
{ | |
if (value == _isChecked) | |
return; | |
_isChecked = value; | |
if (updateChildren && _isChecked.HasValue) | |
{ | |
foreach (CheckedTreeViewItem node in this.Items) | |
{ | |
node.SetIsChecked(_isChecked, true, false); | |
} | |
} | |
if (updateParent && this.Parent != null && this.Parent is CheckedTreeViewItem) | |
((CheckedTreeViewItem)this.Parent).VerifyCheckState(); | |
this.OnPropertyChanged("IsChecked"); | |
} | |
protected void OnPropertyChanged(string property) | |
{ | |
if (PropertyChanged != null) | |
{ | |
PropertyChanged(this, new PropertyChangedEventArgs(property)); | |
} | |
} | |
void VerifyCheckState() | |
{ | |
bool? state = null; | |
for (int i = 0; i < this.Items.Count; ++i) | |
{ | |
bool? current = ((CheckedTreeViewItem)this.Items[i]).IsChecked; | |
if (i == 0) | |
{ | |
state = current; | |
} | |
else if (state != current) | |
{ | |
state = null; | |
break; | |
} | |
} | |
this.SetIsChecked(state, false, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment