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>

   
    
    
     


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>

   
    
    
     


TreeView And TreeViewItem


   
   

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
       <HeaderedItemsControl Header="TreeView">
        <StackPanel>
          <TreeView Width="150" Height="150" Margin="8">
            <TreeViewItem Header="Top One">
              <TreeViewItem Header="Sub One" />
              <TreeViewItem Header="Sub Two (Longer than Normal)" />
              <TreeViewItem Header="Sub Three" />
              <TreeViewItem Header="Sub Four">
                <TreeViewItem Header="Sub One" />
                <TreeViewItem Header="Sub Two" />
                <TreeViewItem Header="Sub Three" />
              </TreeViewItem>
              <TreeViewItem Header="Sub Five" />
            </TreeViewItem>
            <TreeViewItem Header="Top Two">
              <TreeViewItem Header="Sub One" />
              <TreeViewItem Header="Checkable" />
              <TreeViewItem Header="Sub Three" />
              <TreeViewItem Header="Sub Four">
                <TreeViewItem Header="Sub One" />
                <TreeViewItem Header="Sub Two" />
                <TreeViewItem Header="Sub Three" />
              </TreeViewItem>
              <TreeViewItem Header="Sub Five" />
            </TreeViewItem>
            <TreeViewItem Header="Top Three" >
              <TreeViewItem Header="Sub One" />
              <TreeViewItem Header="Sub Two" />
              <TreeViewItem Header="Sub Three" />
              <TreeViewItem Header="Sub Five" />
            </TreeViewItem>
          </TreeView>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>

   
    
    
     


Set drop shadow for a ToolTip


   
     

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="160" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type ToolTip}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="100"/>
            <Setter Property="HasDropShadow" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ToolTip}">
                        <Border Name="Border"
                            BorderBrush="DarkGray"
                            BorderThickness="1"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            CornerRadius="4">
                            <Border.Background>
                                <LinearGradientBrush 
                                    StartPoint="0,0"
                                    EndPoint="0,1">
                                    <GradientStop 
                                        Color="Snow" 
                                        Offset="0.0"/>
                                    <GradientStop 
                                        Color="Gainsboro" 
                                        Offset="1.0"/>
                                </LinearGradientBrush>
                            </Border.Background>

                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <Grid>
            <TextBlock Foreground="DarkGray" 
                       VerticalAlignment="Center" 
                       HorizontalAlignment="Center"
                       ToolTip="This is a custom tooltip"
                       Text="Mouse Over for tooltip"/>
    </Grid>
    
</Window>

   
    
    
    
    
     


Add Image to ToolTip


   
     



<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="160" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type ToolTip}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="100"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ToolTip}">
                        <Border Name="Border"
                            BorderBrush="DarkGray"
                            BorderThickness="1"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            CornerRadius="4">
                            <StackPanel Orientation="Horizontal">
                                <Image Margin="4,4,0,4" Source="c:image.gif"/>
                                <ContentPresenter
                                  Margin="4" 
                                  HorizontalAlignment="Left"
                                  VerticalAlignment="Top" />
                                </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <Grid>
            <TextBlock Foreground="DarkGray" 
                       VerticalAlignment="Center" 
                       HorizontalAlignment="Center"
                       ToolTip="This is a custom tooltip"
                       Text="Mouse Over for tooltip"/>
    </Grid>
    
</Window>

   
    
    
    
    
     


Set border for ToolTip by using ControlTemplate


   
     

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="160" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type ToolTip}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="100"/>
            <Setter Property="HasDropShadow" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ToolTip}">
                        <Border Name="Border"
                            BorderBrush="DarkGray"
                            BorderThickness="1"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            CornerRadius="4">
                            <Border.Background>
                                <LinearGradientBrush 
                                    StartPoint="0,0"
                                    EndPoint="0,1">
                                    <GradientStop 
                                        Color="Snow" 
                                        Offset="0.0"/>
                                    <GradientStop 
                                        Color="Gainsboro" 
                                        Offset="1.0"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <Grid>
            <TextBlock Foreground="DarkGray" 
                       VerticalAlignment="Center" 
                       HorizontalAlignment="Center"
                       ToolTip="This is a custom tooltip"
                       Text="Mouse Over for tooltip"/>
    </Grid>
    
</Window>

   
    
    
    
    
     


Complex ToolTip


   
      
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF" 
  Background="OrangeRed">
        <Canvas>
        <CheckBox>
          CheckBox
          <CheckBox.ToolTip>
            <StackPanel>
              <Label FontWeight="Bold" Background="Blue" Foreground="White">
                The CheckBox
              </Label>
              <TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200">
                this is a test
              </TextBlock>
              <Line Stroke="Black" StrokeThickness="1" X2="200"/>
              <StackPanel Orientation="Horizontal">
                <Image Margin="2" Source="help.png"/>
                <Label FontWeight="Bold">Press F1 for more help.</Label>
              </StackPanel>
            </StackPanel>
          </CheckBox.ToolTip>
        </CheckBox>
        </Canvas>

</Window>