Handles the Selected event for all TreeViewItems : TreeView « Windows Presentation Foundation « VB.Net Tutorial






<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    <DockPanel LastChildFill="True">
        <DockPanel.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <EventSetter Event="Selected" Handler="TreeViewItem_Selected" />
            </Style>
        </DockPanel.Resources>
        <Button Click="Button_Click" DockPanel.Dock="Bottom" Content="Show Selected" MaxHeight="23" MaxWidth="100" />
        <TreeView FontSize="16" Name="tvTree">
            <TreeViewItem Header="A" IsExpanded="True">
                <TreeViewItem Header="1">
                    <TreeViewItem Header="2" />
                    <TreeViewItem Header="3" />
                </TreeViewItem>
                <TreeViewItem Header="B" IsExpanded="True">
                    <TreeViewItem Header="11" />
                    <TreeViewItem Header="22" />
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="C">
                <TreeViewItem Header="E">
                    <TreeViewItem Header="111" />
                    <TreeViewItem Header="222" />
                    <TreeViewItem Header="333" />
                </TreeViewItem>
                <TreeViewItem Header="F">
                    <TreeViewItem Header="1111" />
                    <TreeViewItem Header="2222" />
                    <TreeViewItem Header="333" />
                </TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </DockPanel>
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub TreeViewItem_Selected(sender As Object, e As RoutedEventArgs)
      Dim item As TreeViewItem = TryCast(sender, TreeViewItem)
      If item Is e.OriginalSource Then
        Console.WriteLine(item.Header)
        Console.WriteLine(item.Items.Count)
      Else
        Console.WriteLine("Parent of selected")
        Console.WriteLine(item.Header)
        Console.WriteLine(item.Items.Count)
      End If
    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
      Dim item As TreeViewItem = TryCast(tvTree.SelectedItem, TreeViewItem)

      If item IsNot Nothing Then
        MessageBox.Show("Item selected: " & Convert.ToString(item.Header), Title)
      Else
        MessageBox.Show("No item selected", Title)
      End If
    End Sub
  End Class
End Namespace
WPF Handles The Selected Event For All Tree View Items








16.26.TreeView
16.26.1.Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView.Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView.
16.26.2.Handles the Selected event for all TreeViewItemsHandles the Selected event for all TreeViewItems
16.26.3.Set TreeView with TreeViewItemSet TreeView with TreeViewItem
16.26.4.View and Select Items Using a TreeView and Select Items Using a Tree
16.26.5.Get selected tree node itemGet selected tree node item