Instead of using the TreeViewItem class of objects I ended up just creating the following class:
using System.Collections.ObjectModel;
namespace Custom_XML_Editor.Models
{
public class TreeNode
{
public string Header
{
get;
set;
}
public ObservableCollection<TreeNode> Children
{
get;
set;
}
public TreeNode(string header)
{
Header = header;
Children = new();
}
}
}
Then modifying the XAML:
<UserControl x:Class="Custom_XML_Editor.Views.XMLTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Custom_XML_Editor.Views"
xmlns:models="clr-namespace:Custom_XML_Editor.Models"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<Grid>
<TreeView ItemsSource="{Binding Root}"
Grid.Row="1">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:TreeNode}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Header}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</UserControl>
It seems silly that using the TreeViewItems class is.... impossible(?) to add items to a TreeView pragmatically from a ViewModel, but a generic class you throw together in 20 seconds works just fine.