TreeView with DataSource : TreeView « Windows Presentation Foundation « C# / C Sharp






TreeView with DataSource

TreeView with DataSource
   
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Authors and Their Books">
    <Page.Resources>
        <XmlDataProvider x:Key="data" XPath="Authors">
            <x:XData>
                <Authors xmlns="">
                    <Author Name="Jane Austen">
                        <BirthDate>1972</BirthDate>
                        <DeathDate>2010</DeathDate>
                        <Books>
                            <Book Title="A">
                                <PubDate>2003</PubDate>
                            </Book>
                            <Book Title="B">
                                <PubDate>1813</PubDate>
                            </Book>
                        </Books>
                    </Author>
                </Authors>
            </x:XData>
        </XmlDataProvider>
        <HierarchicalDataTemplate DataType="Author" ItemsSource="{Binding XPath=Books/Book}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding XPath=@Name}" />
                <TextBlock Text=" (" />
                <TextBlock Text="{Binding XPath=BirthDate}" />
                <TextBlock Text="-" />
                <TextBlock Text="{Binding XPath=DeathDate}" />
                <TextBlock Text=")" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="Book">
            <StackPanel Orientation="Horizontal" TextBlock.FontSize="10pt">
                <TextBlock Text="{Binding XPath=@Title}" />
                <TextBlock Text="{Binding XPath=PubDate}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </Page.Resources>
    <TreeView ItemsSource="{Binding Source={StaticResource data}, XPath=Author}" />
</Page>

   
    
    
  








Related examples in the same category

1.TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.
2.TreeView And TreeViewItemTreeView And TreeViewItem
3.Create a Tree and add childrenCreate a Tree and add children
4.Add Ellipse, TextBlock to a TreeAdd Ellipse, TextBlock to a Tree
5.Mark Tree node expandableMark Tree node expandable
6.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.
7.Handles the Selected event for all TreeViewItemsHandles the Selected event for all TreeViewItems
8.Set TreeView with TreeViewItemSet TreeView with TreeViewItem
9.View and Select Items Using a TreeView and Select Items Using a Tree
10.Get selected tree node itemGet selected tree node item