question

Allanjb avatar image
Allanjb asked ·

UWP TreeView Example demonstating Data Binding throws a Win32 Exception.

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

https://docs.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


windows-uwp-xamlwindows-uwp-feedback
msquestion.txt (7.3 KiB)
4 comments
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you the native TreeView control from Windows.UI.Xaml.Controls namespace instead of WinUI, will the same issue occur?

0 Votes 0 · ·

Hi: The TreeView control is a Windows.UI.Xaml.Controls control so I reference that namespace, exactly as described in the Microsoft documentation, (see the link in my question above). I would be interested in the result of someone building the code as I have included in the attachment to see if they get the same result.


0 Votes 0 · ·

From your attachment, what you used is the TreeView control which is from Windows UI Library and the namespace is Microsoft.UI.Xaml.Controls. When I tested the code from your attachment, I can receive the same issue. However, if I used the native TreeView control from Windows.UI.Xaml.Controls namespace, it worked well. So maybe you could use the native TreeView control directly or report this issue in the winui-github.


0 Votes 0 · ·

@Allanjb Have you checked @PeterFleischer-3316 's answer? Does it solve your problem?

0 Votes 0 · ·

1 Answer

PeterFleischer-3316 avatar image
PeterFleischer-3316 answered ·

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



x.png (174.6 KiB)
x.gif (342.3 KiB)
Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.