We are converting a WinForms application to WPF. so I need to accept the input like WinForms. As I don't want to change the logic of it. Ao I write the class for those its works. But the is not good please take a look at the class once and suggest the edits in this class.
public class TreeItem : INotifyPropertyChanged
{
private TreeItem()
{
if (Nodes == null)
Nodes = new TreeItems();
}
private TreeItem(string DisplayName, string IdName) : this()
{
this.DisplayName = DisplayName;
this.IdName = IdName;
}
public TreeItem(string DisplayName, string IdName, TreeItem parent = null) : this(DisplayName, IdName)
{
Parent = parent;
}
bool isChecked = false;
private string idName = string.Empty;
string name = string.Empty;
private object tag;
public object Tag
{
get { return tag; }
set { tag = value; }
}
public bool IsChecked
{
get
{
return isChecked;
}
set
{
isChecked = value;
OnPropertyChanged("IsChecked");
}
}
public string IdName
{
get
{
return idName;
}
set
{
idName = value;
OnPropertyChanged("IdName");
}
}
public string DisplayName
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged("Name");
}
}
public TreeItem Parent { get; set; }
private TreeItems nodes;
public TreeItems Nodes
{
get
{
return nodes;
}
set
{
nodes = value;
foreach (var item in nodes)
{
item.Parent = this;
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null && !string.IsNullOrEmpty(propertyName))
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class TreeItems : IList<TreeItem>
{
public TreeItem this[string itemName]
{
get
{
return GetNode(allParents, itemName);
}
}
public TreeItem GetNode(List<TreeItem> currentList, string name)
{
foreach (var item in currentList)
{
if (item.IdName == name)
{
return item;
}
}
foreach (var item in currentList)
{
TreeItem treeItem = GetNode(item.Nodes.allParents, name);
if (treeItem != null)
{
return treeItem;
}
}
return null;
}
List<TreeItem> allParents = new List<TreeItem>();
public TreeItem this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public int Count => throw new NotImplementedException();
public bool IsReadOnly => throw new NotImplementedException();
public void Add(TreeItem item)
{
allParents.Add(item);
}
public void GetParent() { }
public void Clear()
{
allParents.Clear();
}
public bool Contains(TreeItem item)
{
throw new NotImplementedException();
}
public void CopyTo(TreeItem[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int IndexOf(TreeItem item)
{
throw new NotImplementedException();
}
public void Insert(int index, TreeItem item)
{
throw new NotImplementedException();
}
public bool Remove(TreeItem item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public IEnumerator<TreeItem> GetEnumerator()
{
return allParents.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
the way im assigning the parent is bad. so any ideas will be helpful thanks.
Usage:
TreeItems tvLieferantsItems ;
tvLieferantsItems[betr["PARTID"].ToString()]?.Nodes[betr["BETID"]?.ToString()]?.Nodes[betr["BERID"].ToString()]?.Nodes.Add(abt);