UWP TreeView Example demonstating Data Binding throws a Win32 Exception.

Allanjb 246 Reputation points
2020-09-03T15:21:07.133+00:00

There is a UWP TreeView example in the Microsoft documentation at:

https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/tree-view

I cannot get this example to run without getting an exception every time I try and select a checkbox.
The exception reads: An unhandled win32 exception occurred in [20652] TreeViewDemo.exe.
I am not able to debug this exception or continue.

I have included my source in a text file attachment.

22430-msquestion.txt

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,321 Reputation points
    2020-09-14T05:12:41.717+00:00

    Hi,
    instead of using Microsoft.UI.Xaml.Controls use Windows.UI.Xaml.Controls and Windows Version 17763 or higher. Try following demo:

    XAML

    24315-x.png

    And code:

    using System.Collections.ObjectModel;  
    using Windows.UI.Xaml;  
    using Windows.UI.Xaml.Controls;  
      
    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409  
      
    namespace App17  
    {  
      /// <summary>  
      /// An empty page that can be used on its own or navigated to within a Frame.  
      /// </summary>  
      public sealed partial class Page17 : Page  
      {  
        private ObservableCollection<Item> DataSource = new ObservableCollection<Item>();  
      
        public Page17()  
        {  
          this.InitializeComponent();  
          DataSource = GetDessertData();  
        }  
      
        private ObservableCollection<Item> GetDessertData()  
        {  
          var list = new ObservableCollection<Item>();  
      
          Item flavorsCategory = new Item()  
          {  
            Name = "Flavors",  
            Children =  
                        {  
                            new Item() { Name = "Vanilla" },  
                            new Item() { Name = "Strawberry" },  
                            new Item() { Name = "Chocolate" }  
                        }  
          };  
      
          Item toppingsCategory = new Item()  
          {  
            Name = "Toppings",  
            Children =  
                        {  
                            new Item()  
                            {  
                                Name = "Candy",  
                                Children =  
                                {  
                                    new Item() { Name = "Chocolate" },  
                                    new Item() { Name = "Mint" },  
                                    new Item() { Name = "Sprinkles" }  
                                }  
                            },  
                            new Item()  
                            {  
                                Name = "Fruits",  
                                Children =  
                                {  
                                    new Item() { Name = "Mango" },  
                                    new Item() { Name = "Peach" },  
                                    new Item() { Name = "Kiwi" }  
                                }  
                            },  
                            new Item()  
                            {  
                                Name = "Berries",  
                                Children =  
                                {  
                                    new Item() { Name = "Strawberry" },  
                                    new Item() { Name = "Blueberry" },  
                                    new Item() { Name = "Blackberry" }  
                                }  
                            }  
                        }  
          };  
      
          list.Add(flavorsCategory);  
          list.Add(toppingsCategory);  
          return list;  
        }  
      
        private void OrderButton_Click(object sender, RoutedEventArgs e)  
        {  
          FlavorList.Text = string.Empty;  
          ToppingList.Text = string.Empty;  
      
          foreach (TreeViewNode node in DessertTree.SelectedNodes)  
          {  
            if (node.Parent.Content?.ToString() == "Flavors")  
            {  
              FlavorList.Text += node.Content + "; ";  
            }  
            else if (node.HasChildren == false)  
            {  
              ToppingList.Text += node.Content + "; ";  
            }  
          }  
        }  
      
        private void SelectAllButton_Click(object sender, RoutedEventArgs e)  
        {  
          if (DessertTree.SelectionMode == TreeViewSelectionMode.Multiple)  
          {  
            DessertTree.SelectAll();  
          }  
        }  
      }  
      
      public class Item  
      {  
        public string Name { get; set; }  
        public ObservableCollection<Item> Children { get; set; } = new ObservableCollection<Item>();  
      
        public override string ToString()  
        {  
          return Name;  
        }  
      }  
    }  
    

    24354-x.gif

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.