So much like a Header is a property of a TreeViewItem, I am trying to add another property that I can reference for my code.
I started looking into DependencyProperty, but I have a feeling this is the wrong direction, as well as making a simple extension to try and set the property I am looking for.
The little snippet that I’m trying to get to work looks a little like this:
public bool AddNewTreeviewChild(ItemCollection selectedTreeView, TreeViewItem selectedItem)
{
if (selectedTreeView.Contains(selectedItem))
{
var newChild = new TreeViewItemExtension();
newChild.Header = NewItem.Text;
newChild.TreeViewType = DesignateChildType();
selectedItem.Items.Add(newChild);
}
foreach (TreeViewItem item in selectedTreeView)
{
if (AddNewTreeviewChild(item.Items, selectedItem))
{
return true;
}
}
return false;
}
The dependency Property way I tried:
public static readonly DependencyProperty TreeViewTypeProp =
DependencyProperty.Register(
"TreeViewType",
typeof(string),
typeof(TreeViewItem),
new PropertyMetadata("Root")
);
public string TreeViewType
{
get { return (string)GetValue(TreeViewTypeProp); }
set { SetValue(TreeViewTypeProp, value); }
}
However, this still makes the TreeViewType inaccessible or unsable in the above example, thus not compiling or running.
I also attempted doing an extension with a simple:
public class TreeViewItemExtension : TreeViewItem
{
public string TreeViewType { get; set; }
public TreeViewItemExtension ()
{
}
}
But when looking at newly added items, the TreeViewType is missing. I tried casting and renaming various TreeViewItems into TreeViewItemExtension, but had no luck. What am I missing?