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>

   
    
    
     


ToolTip With Binding


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplication1" 
  Title="WithBinding" Height="135" Width="200">
  <Window.Resources>
    <local:Employee x:Key="Tom" Name="Tom" Age="11" />
    <local:AgeToForegroundConverter x:Key="ageConverter" />
    <local:Base16Converter x:Key="Base16Converter" />
  </Window.Resources>
  <StackPanel DataContext="{StaticResource Tom}">
    <TextBlock>Name:</TextBlock>
    <TextBox Text="{Binding Path=Name}" />
    <TextBox Text="{Binding Path=Name, Source={StaticResource Tom}}" />
    <TextBlock>Age:</TextBlock>
    <TextBox>
      <TextBox.Text>
        <Binding Path="Age" />
      </TextBox.Text>
    </TextBox>
    <TextBox Name="ageTextBox" Text="{Binding Path=Age}"
             Foreground="{Binding Path=Age, Converter={StaticResource ageConverter}}" />
    <TextBox Text="{Binding Path=Age, Converter={StaticResource Base16Converter}}"
             Foreground="{Binding Path=Age, Converter={StaticResource ageConverter}}" />
    <TextBox Name="ageTextBoxOneWay" Foreground="{Binding Path=Age, Mode=OneWay,Source={StaticResource Tom}, Converter={StaticResource ageConverter}}"
      ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)&#91;0&#93;.ErrorContent}">
      <TextBox.Text>
        <Binding Path="Age" UpdateSourceTrigger="PropertyChanged">
          <Binding.ValidationRules>
            <local:NumberRangeRule Min="0" Max="128" />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>
    </TextBox>
    <Button Name="birthdayButton" Foreground="{Binding Path=Foreground, ElementName=ageTextBox}">Birthday</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Diagnostics;


namespace WpfApplication1 {

  public class Employee : INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propName) {
      if( this.PropertyChanged != null ) {
        PropertyChanged(this, new PropertyChangedEventArgs(propName));
      }
    }

    string name;
    public string Name {
      get { return this.name; }
      set {
        this.name = value;
        Notify("Name");
      }
    }

    int age;
    public int Age {
      get { return this.age; }
      set {
        this.age = value;
        Notify("Age");
      }
    }

    public Employee() { }
    public Employee(string name, int age) {
      this.name = name;
      this.age = age;
    }
  }

  public partial class Window1 : Window {
    public Window1() {
      InitializeComponent();
      this.birthdayButton.Click += birthdayButton_Click;
    }
    void birthdayButton_Click(object sender, RoutedEventArgs e) {
      Employee emp = (Employee)this.FindResource("Tom");
      ++emp.Age;
      Console.WriteLine(emp.Name);
      Console.WriteLine(emp.Age);
    }
  }
  [ValueConversion(/* sourceType */ typeof(int), /* targetType */ typeof(Brush))]
  public class AgeToForegroundConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      if( targetType != typeof(Brush) ) { return null; }
      int age = int.Parse(value.ToString());
      return (age > 60 ? Brushes.Red : Brushes.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      throw new NotImplementedException();
    }
  }

  public class Base16Converter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return ((int)value).ToString("x");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return int.Parse((string)value, System.Globalization.NumberStyles.HexNumber);
    }
  }

  public class NumberRangeRule : ValidationRule {
    int _min;
    public int Min {
      get { return _min; }
      set { _min = value; }
    }

    int _max;
    public int Max {
      get { return _max; }
      set { _max = value; }
    }

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) {
      int number;
      if( !int.TryParse((string)value, out number) ) {
        return new ValidationResult(false, "Invalid number format");
      }

      if( number < _min || number > _max ) {
        string s = string.Format("Number out of range ({0}-{1})", _min, _max);
        return new ValidationResult(false, s);
      }
      return ValidationResult.ValidResult;
    }
  }
}

   
    
     


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>

   
    
    
    
    
    
     


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>

   
    
    
    
    
     


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 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>

   
    
    
    
    
     


Set ToolTipService.Placement=”Center”


   
     

<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="200" Width="300">
    <StackPanel>

        <Button Height="40" Margin="5" ToolTipService.Placement="Center">
            <Button.ToolTip>
                <ToolTip>
                    ToolTip displayed for 5 seconds...
                </ToolTip>
            </Button.ToolTip>
            Button with Centered ToolTip
        </Button>
    </StackPanel>
</Window>