question

47290974 avatar image
0 Votes"
47290974 asked RoyLi-MSFT edited

UWP TreeView SelectItem MVVM

Hello
I want to get the selected information from the treeview from the ViewModel when the button is pressed.

<winui:TreeView HorizontalAlignment="Left"
ItemsSource="{Binding TreeDataSource}"
Grid.Column="0"
Background="Aqua">
<winui:TreeView.ItemTemplate>
<DataTemplate>
<winui:TreeViewItem ItemsSource="{Binding Childrens}" Content="{Binding Name}" IsExpanded="True"/>
</DataTemplate>
</winui:TreeView.ItemTemplate>

</winui:TreeView>

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 is the button when you said when the button is pressed? Do you mean the TreeViewItem?

0 Votes 0 ·

1 Answer

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered RoyLi-MSFT edited

Hello,

Welcome to Microsoft Q&A!

TreeView has TreeView.SelectedItem Property and TreeView.SelectedItems Property. You can use these properties to get the content of selected nodes directly. If multiple selections are enabled, SelectedItem contains the first item in the SelectedItems collection.

Like this:

 MyDataModel selectedItem= myTreeView.SelectedItem as MyDataModel 

For more information about TreeView, you could refer to TreeViews in UWP Apps.

Thank you.


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.



· 2
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.

Thank you !

Can I ask you just one more thing?

 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  {
                 var item_name = objTostr(ds.Tables[0].Rows[i]["Name"]);
                 var item_code = objTostr(ds.Tables[0].Rows[i]["Code"]);
    
                 db_list.Add(new ExploreItem() { Name = item_name, Code = item_code });
 }
    
             var list = new ObservableCollection<ExploreItem>();
             ExploreItem folder1 = new ExploreItem()
             {
                 Name = "시스템코드",
                 Code = "",
                 Childrens = db_list
    
             };
             list.Add(folder1);
    
             return list;

I bound it to the treeview like this

What I want to do is to display the results on the data grid with the selected information in the tree view when the search button is pressed. To do that, we need to know the code of the selected node in the ViewModel.

  private object _selectedItem = new object();
         public object SelectedItem
         {
             get { return _selectedItem; }
             set { SetProperty(ref _selectedItem, value); }
         }


If I write code like this, can I access 'Code'?

Sorry for my poor English



0 Votes 0 ·

Please ask another question in a new thread so that we could keep one question in one thread. I and my teammates will check your new thread.

0 Votes 0 ·