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
the solution in Expression Blend 3.
Go ahead and a Tree View control to
I have changed the Background to Linear
To add TreeView Item to the Tree View, just right click on it and
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
you see the design view it will look similar to the following:
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
Add an event SelectedItemChanged for the TreeView.
to the Event Handler and add the following code:
private void MyTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs
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:
it, it was so simple to achieve that.