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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Windows Presentation Foundation » TreeViewScreenshots 
Handles the Selected event for all TreeViewItems
Handles the Selected event for all TreeViewItems
     
<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

   
    
    
    
    
  
Related examples in the same category
1.Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView.
2.Set TreeView with TreeViewItemSet TreeView with TreeViewItem
3.View and Select Items Using a TreeView and Select Items Using a Tree
4.Get selected tree node itemGet selected tree node item
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.