Set TreeView with TreeViewItem : TreeView « Windows Presentation Foundation « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Windows Presentation Foundation » TreeView 
16.26.3.Set TreeView with TreeViewItem
<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 Set Tree View With Tree View Item
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.