Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView. : TreeView « Windows Presentation Foundation « VB.Net






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

   
    
    
    
    
  








Related examples in the same category

1.Handles the Selected event for all TreeViewItemsHandles the Selected event for all TreeViewItems
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