question

Voytec avatar image
0 Votes"
Voytec asked AryaDing-MSFT edited

UWP C# TreeView getting selected node value

 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?

windows-uwp
· 1
5 |1600 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.

What errors?

0 Votes 0 ·

1 Answer

AryaDing-MSFT avatar image
1 Vote"
AryaDing-MSFT answered AryaDing-MSFT edited

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
5 |1600 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.

It's working!
Thank You so much Arya!

0 Votes 0 ·