Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView. : 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 Tutorial » Windows Presentation Foundation » TreeView 
16.26.1.Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView.
<Window x:Class="TreeViewDataBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TreeViewDataBinding"
    xmlns:EngNews="clr-namespace:TreeViewDataBinding">

  <Window.Resources>
    <ObjectDataProvider x:Key="EasternHemisphereDataSource" ObjectType="{x:Type EngNews:WebSiteGroupA}"/>
    <ObjectDataProvider x:Key="WesternHemisphereDataSource" ObjectType="{x:Type EngNews:WebSiteGroupB}"/>
    <DataTemplate x:Key="NewspaperTVItem">
      <DockPanel>
        <Image Source="c:\image.jpg"/>
        <TextBlock VerticalAlignment="center" Text ="{Binding Path=Name}"/>
      </DockPanel>
    </DataTemplate>
    <DataTemplate x:Key="WesternHemisphereHeaderTemplate">
      <DockPanel>
        <Ellipse Width="7" Height="7"  Fill="Blue" DockPanel.Dock="Left"/>
        <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" DockPanel.Dock="Right">
          test
        </TextBlock>
      </DockPanel>
    </DataTemplate>
    <DataTemplate x:Key="EasternHemisphereHeaderTemplate">
      <DockPanel>
        <Ellipse Width="7" Height="7"  Fill="Blue" DockPanel.Dock="Left"/>
        <TextBlock DockPanel.Dock="Right">Eastern Hemisphere</TextBlock>
      </DockPanel>
    </DataTemplate>
  </Window.Resources>
  <StackPanel>
      <TreeView Name="WebSiteEntrys" SelectedItemChanged="SelectedNewspaperChanged">
        <TreeViewItem HeaderTemplate="{StaticResource WesternHemisphereHeaderTemplate}" 
                      ItemsSource="{Binding Source={StaticResource WesternHemisphereDataSource}}"
                      ItemTemplate="{StaticResource NewspaperTVItem}"/>
        <TreeViewItem HeaderTemplate="{StaticResource EasternHemisphereHeaderTemplate}"
                      ItemsSource="{Binding Source={StaticResource EasternHemisphereDataSource}}"
                      ItemTemplate="{StaticResource NewspaperTVItem}"/>
      </TreeView>
    <Frame Name = "NewspaperFrame" Content="this is a test."/>
  </StackPanel>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Collections.ObjectModel

Namespace TreeViewDataBinding
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub SelectedNewspaperChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of [Object]))
      Dim engnews As WebSiteEntry = TryCast(WebSiteEntrys.SelectedItem, WebSiteEntry)
      If engnews IsNot Nothing Then
        NewspaperFrame.Navigate(New System.Uri(engnews.Website))
      End If
    End Sub
  End Class
  Public Class WebSiteEntry
    Private _name As String
    Private _website As String

    Public Property Website() As String
      Get
        Return _website
      End Get
      Set
        _website = value
      End Set
    End Property

    Public Property Name() As String
      Get
        Return _name
      End Get
      Set
        _name = value
      End Set
    End Property

    Public Sub New(name__1 As String, website__2 As String)
      Name = name__1
      Website = website__2
    End Sub
  End Class
  Public Class WebSiteGroupA
    Inherits ObservableCollection(Of WebSiteEntry)
    Public Sub New()
      Add(New WebSiteEntry("A""http://www.A.com"))
      Add(New WebSiteEntry("B""http://www.B.com"))
      Add(New WebSiteEntry("C""http://www.C.com"))
    End Sub
  End Class

  Public Class WebSiteGroupB
    Inherits ObservableCollection(Of WebSiteEntry)
    Public Sub New()
      Add(New WebSiteEntry("D""http://www.D.com/"))
      Add(New WebSiteEntry("E""http://www.E.net/"))
      Add(New WebSiteEntry("F""http://www.F.com"))
    End Sub
  End Class
End Namespace
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.