Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Last active May 6, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiannagel/b84d6623419e8d977ced48574bad622d to your computer and use it in GitHub Desktop.
Save christiannagel/b84d6623419e8d977ced48574bad622d to your computer and use it in GitHub Desktop.
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