Auto-generate name for a custom TreeViewItem

334 Views Asked by At

I have a class derived from TreeviewItem(Custom TreeViewItem) which is used by a class derived from Treeview(custom Treeview Control).
When the user of my control adds items to my custom TreeView using the default design-time collection editor I want the newly added items to have a default name (like MyNode1, MyNode2 etc) and I want that to be written in XAML.
Any ideas on how to do that?
I noticed that when you drop a control from the toolbox it has a default name assigned to it. I would like to reuse this existing mechanism in Visual Studio 2010 if possible for my tree node classes.

1

There are 1 best solutions below

3
On BEST ANSWER

TreeViews and TreeViewItems are user interface controls and should not be used to stored any kind of information that needs to be saved. Instead, you should create a hierarchical data type to store your information, bind that to the TreeView.ItemsSource property and provide a HierarchicalDataTemplate to display the data as you wish.

This way, you can add new items to the TreeView by simply adding new data type objects to your bound collection in your view model or code behind. You can assign whatever values that you want to the objects before you add them to the collection and they will all be accessible from the collection at anmy time.