UWP C# TreeView getting selected node value

VoyTec 671 Reputation points
2021-05-16T19:35:16.9+00:00
private void szkloClick(Microsoft.UI.Xaml.Controls.TreeView sender, Microsoft.UI.Xaml.Controls.TreeViewItemInvokedEventArgs args)
        {
            TreeViewNode select = (szklo.SelectedNode as TreeViewNode);
                switch (select.Content.ToString())
            {
                case "33.2":
                    szklotekst.Text = "33.2";
                    break;
            }
        }

Following code gives me errors.
How to get the value of selected node and put it into TextBlock?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,916 Reputation points
    2021-05-17T02:19:27.187+00:00

    Hi,

    Welcome to Microsoft Q&A!

    In the code-behind, you could add this using statement at the top of the code:
    using muxc = Microsoft.UI.Xaml.Controls;

    In addition, the ItemInvoked event args give you access to the invoked item. The InvokedItem property has the node that was invoked. You can cast it to a TreeViewNode and get the data item from the TreeViewNode.Content property. As follows:

    private void myTreeView_ItemInvoked(muxc.TreeView sender, muxc.TreeViewItemInvokedEventArgs args)  
            {  
               var node = args.InvokedItem as muxc.TreeViewNode;  
               myTextBlock.Text= node.Content.ToString();  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

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.