Have read many articles re wpf TreeView binding but have failed to find how to at runtime save so as when program is used next time treeview items are there.
Have achied this with Winforms by binary formatter and serialization
Treeview is populated at run time and would like a button that when clicked it saves the treeview or treeview data
Please point me in the right direction
Not an example with hardcoded items
Thank you
Peter
Anandu G NathPosted Dec 21, 2023, 5:35 AM
Here's an example demonstrating how to create a simple WPF
C# Code
Jayraj ChhayaPosted Dec 18, 2023, 5:45 AM
To save a WPF TreeView at runtime and preserve its items for future use, you can follow these steps:
Create a data structure to hold the treeview data. This can be a custom class or a collection of objects that represent the treeview items.
Bind the TreeView control to the data structure using data binding. This allows the treeview to automatically update its items based on changes to the data structure.
When the user clicks the save button, serialize the data structure to a file using a serialization method such as XML or JSON. This will save the treeview data in a format that can be easily loaded later.
To load the saved treeview data, deserialize the file and populate the data structure with the saved data.
Here's an example of how you can implement this in WPF using C#:
By following these steps and using data binding and serialization, you can save and load the WPF TreeView at runtime.
Sam HobbsPosted Dec 16, 2023, 9:22 PM
You should have a collection, such as a list or a table, with the data that you bind to the TreeView. Then it is a matter of serializing the collection. Look for articles about self-referencing tables and hierarchical tables. A good way to create and manage such a collection is to use Entity Framework.
I have the following in my bookmarks:
c# - How to bind Self-referencing table to WPF TreeView - Stack Overflow
https://stackoverflow.com/questions/14161963/how-to-bind-self-referencing-table-to-wpf-treeview
TreeView and HierarchicalDataTemplate, Step-by-Step | Microsoft Learn
https://learn.microsoft.com/en-us/archive/blogs/mikehillberg/treeview-and-hierarchicaldatatemplate-step-by-step