.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Select Tree View Nodes In Silverlight Application

Posted By:Diptimaya Patra       Posted Date: April 04, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

Select Tree View Nodes In Silverlight Application. In this article we will see how can we select the Node name and display which node you have selected.


In my previous article on TreeView titled "Create Treeview in Silverlight"; we explored a simple TreeView with some Parent and Child Nodes. In this article we will see how can we select the Node name and display which node you have selected.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as TreeViewSL3.


Open the solution in Expression Blend 3.

Go ahead and a Tree View control to your application.

I have changed the Background to Linear Brush.

To add TreeView Item to the Tree View, just right click on it and add TreeViewItem.


Add several, and if you want to add children of any parent then select the particular TreeViewItem and add another Treeview Item.

I have created the below hierarchy:


If you see the design view it will look similar to the following:


Here is the XAML code behind for the TreeView:


Now we will add a TextBlock which will display which node is been selected. Follow the Xaml code:

We can achieve the objective in both XAML code and C# code. For the time being I will use the C# code behind.

Add an event SelectedItemChanged for the TreeView.

Now navigate to the Event Handler and add the following code:

private void  MyTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs  e)
TreeView view = sender as TreeView;
if (view != null)
SelectedValue.Text= (view.SelectedItem as TreeViewItem).Header.ToString();
As you see from the above code I have created an instance of the TreeView and if it's value us not null; I am typecasting the object to a TreeViewItem. Then using the Header property I am assigning it to a TextBlock's Text property.

If you run your application you can see the change:


That's it, it was so simple to achieve that.

Enjoy Coding.
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend