Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Last active May 6, 2018 18:09
Embed
What would you like to do?
Fill the TreeView with sample data
public sealed partial class MainPage : Page
{
public MainPage() => InitializeComponent();
private void OnSelectionChanged(TreeView sender, TreeViewItemInvokedEventArgs args)
{
if (args.InvokedItem is TreeViewNode node && node.Content is CultureData cd)
{
ViewModel.SelectedCulture = cd;
}
}
public CulturesViewModel ViewModel { get; } = new CulturesViewModel();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
void AddSubNodes(TreeViewNode parent)
{
if (parent.Content is CultureData cd && cd.SubCultures != null)
{
foreach (var culture in cd.SubCultures)
{
var node = new TreeViewNode
{
Content = culture
};
parent.Children.Add(node);
foreach (var subCulture in culture.SubCultures)
{
AddSubNodes(node);
}
}
}
}
base.OnNavigatedTo(e);
var rootNodes = ViewModel.RootCultures.Select(cd => new TreeViewNode
{
Content = cd
});
foreach (var node in rootNodes)
{
treeView1.RootNodes.Add(node);
AddSubNodes(node);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment