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

Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2023, 4:22 PM EST
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
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 18, 2023, 12:45 AM EST
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#:
// Define a class to represent the treeview items public class TreeViewItemData { public string Name { get; set; } public ObservableCollection Children { get; set; } } // Create an instance of the data structure public ObservableCollection TreeViewData { get; set; } = new ObservableCollection(); // Bind the TreeView control to the data structure treeView.ItemsSource = TreeViewData; // Save button click event handler private void SaveButton_Click(object sender, RoutedEventArgs e) { // Serialize the data structure to a file string filePath = "treeviewdata.xml"; XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection)); using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { serializer.Serialize(fileStream, TreeViewData); } } // Load button click event handler private void LoadButton_Click(object sender, RoutedEventArgs e) { // Deserialize the file and populate the data structure string filePath = "treeviewdata.xml"; XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection)); using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { TreeViewData = (ObservableCollection)serializer.Deserialize(fileStream); } }By following these steps and using data binding and serialization, you can save and load the WPF TreeView at runtime.
Anandu G NathRank #681 · 3.5K reputationPosted Dec 21, 2023, 12:35 AM EST
Here's an example demonstrating how to create a simple WPF
C# Code
using System.Collections.ObjectModel; using System.Windows; namespace WpfApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Create sample hierarchical data var root = new TreeNode { Name = "Root" }; var child1 = new TreeNode { Name = "Child 1" }; var child2 = new TreeNode { Name = "Child 2" }; var subChild1 = new TreeNode { Name = "Sub-Child 1" }; var subChild2 = new TreeNode { Name = "Sub-Child 2" }; // Assign child nodes root.Children.Add(child1); root.Children.Add(child2); child1.Children.Add(subChild1); child1.Children.Add(subChild2); // Set the root node as the DataContext of the TreeView treeView.ItemsSource = new ObservableCollection { root }; } } // Define a simple tree node class public class TreeNode { public string Name { get; set; } public ObservableCollection Children { get; } = new ObservableCollection(); } }