Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView. : TreeView « Windows Presentation Foundation « C# / CSharp Tutorial






<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.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace TreeViewDataBinding
{
    public partial class Window1 : Window{
        public Window1(){
            InitializeComponent();
        }
        private void SelectedNewspaperChanged(object sender,RoutedPropertyChangedEventArgs<Object> e){
            WebSiteEntry engnews = WebSiteEntrys.SelectedItem as WebSiteEntry;
            if( engnews != null ) NewspaperFrame.Navigate(new System.Uri(engnews.Website));
        }
    }
    public class WebSiteEntry{
        private string _name;
        private string _website;

        public string Website
        {
            get { return _website; }
            set { _website = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public WebSiteEntry(string name, string website)
        {
            Name = name;
            Website = website;
        }
    }
    public class WebSiteGroupA : ObservableCollection<WebSiteEntry>
    {
        public WebSiteGroupA()
        {
            Add(new WebSiteEntry("A","http://www.A.com"));
            Add(new WebSiteEntry("B","http://www.B.com"));
            Add(new WebSiteEntry("C","http://www.C.com"));
        }
    }

    public class WebSiteGroupB : ObservableCollection<WebSiteEntry>
    {
        public WebSiteGroupB()
        {
            Add(new WebSiteEntry("D","http://www.D.com/"));
            Add(new WebSiteEntry("E","http://www.E.net/"));
            Add(new WebSiteEntry("F","http://www.F.com"));
        }
    }
}
WPF Bind A Tree View To A Data Source And Use Data Template Objects To Customize The Tree View








24.51.TreeView
24.51.1.TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.
24.51.2.TreeView with DataSourceTreeView with DataSource
24.51.3.TreeView And TreeViewItemTreeView And TreeViewItem
24.51.4.Create a Tree and add childrenCreate a Tree and add children
24.51.5.Add Ellipse, TextBlock to a TreeAdd Ellipse, TextBlock to a Tree
24.51.6.Mark Tree node expandableMark Tree node expandable
24.51.7.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.
24.51.8.Handles the Selected event for all TreeViewItemsHandles the Selected event for all TreeViewItems
24.51.9.Set TreeView with TreeViewItemSet TreeView with TreeViewItem
24.51.10.View and Select Items Using a TreeView and Select Items Using a Tree
24.51.11.Get selected tree node itemGet selected tree node item