TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem. : TreeView « Windows Presentation Foundation « C# / C Sharp






TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.

TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.
   

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TreeViewSelectedValue">
  <FrameworkElement.Resources>
    <XmlDataProvider x:Key="myCourseData" XPath="/CourseData">
      <x:XData>
        <CourseData xmlns="">
          <CourseInfo>
            <CourseName>A</CourseName>
            <CourseWorkDay>Monday</CourseWorkDay>
            <CourseWorkDay>Tuesday</CourseWorkDay>
            <CourseWorkDay>Wednesday</CourseWorkDay>
            <CourseWorkDay>Thrusday</CourseWorkDay>
            <CourseWorkDay>Friday</CourseWorkDay>
            <CourseStartTime>8:00am</CourseStartTime>
            <CourseNumber>12345</CourseNumber>
          </CourseInfo>
          <CourseInfo>
            <CourseName>B</CourseName>
            <CourseWorkDay>Monday</CourseWorkDay>
            <CourseWorkDay>Tuesday</CourseWorkDay>
            <CourseStartTime>6:30am</CourseStartTime>
            <CourseNumber>98765</CourseNumber>
          </CourseInfo>
        </CourseData>
      </x:XData>
    </XmlDataProvider>
    <HierarchicalDataTemplate DataType="CourseInfo" ItemsSource ="{Binding XPath=CourseWorkDay}">
      <TextBlock Text="{Binding XPath=CourseName}" />
    </HierarchicalDataTemplate>
  </FrameworkElement.Resources>
  <StackPanel>
    <TreeView ItemsSource="{Binding Source={StaticResource myCourseData}, XPath=CourseInfo}" 
        Name="myTreeView" SelectedValuePath="CourseNumber"/>

    <TextBlock Margin="10">SelectedValuePath: </TextBlock>
    <TextBlock Margin="10" Text="{Binding ElementName=myTreeView, Path=SelectedValuePath}"/>
    <TextBlock Margin="10">SelectedValue: </TextBlock>
    <TextBlock Margin="10" Text="{Binding ElementName=myTreeView, Path=SelectedValue}"/>

  </StackPanel>     

</Page>

   
    
    
  








Related examples in the same category

1.TreeView with DataSourceTreeView with DataSource
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