ToolTip With Binding : ToolTip « Windows Presentation Foundation « C# / CSharp Tutorial






<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)[0].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;
    }
  }
}
WPF Tool Tip With Binding








24.50.ToolTip
24.50.1.ToolTip for BorderToolTip for Border
24.50.2.Using ToolTip for TextBox with TextBox.ToolTip and ToolTip tagUsing ToolTip for TextBox with TextBox.ToolTip and ToolTip tag
24.50.3.Add ToolTip text to a TextBoxAdd ToolTip text to a TextBox
24.50.4.ToolTipService.InitialShowDelayToolTipService.InitialShowDelay
24.50.5.ToolTip with ImageToolTip with Image
24.50.6.ToolTipService.Placement='Bottom'ToolTipService.Placement='Bottom'
24.50.7.ToolTip with List itemsToolTip with List items
24.50.8.A Tool Tip on a Disabled ControlA Tool Tip on a Disabled Control
24.50.9.Disabled Button with ToolTipServiceDisabled Button with ToolTipService
24.50.10.Control the Display Duration and Position of a Tool TipControl the Display Duration and Position of a Tool Tip
24.50.11.Set ToolTip Placement, ShowDuration, VerticalOffsetSet ToolTip Placement, ShowDuration, VerticalOffset
24.50.12.Set ToolTipService.Placement='Center'Set ToolTipService.Placement='Center'
24.50.13.Set drop shadow for a ToolTipSet drop shadow for a ToolTip
24.50.14.Add Image to ToolTipAdd Image to ToolTip
24.50.15.Set border for ToolTip by using ControlTemplateSet border for ToolTip by using ControlTemplate
24.50.16.Complex ToolTipComplex ToolTip
24.50.17.ToolTip With BindingToolTip With Binding
24.50.18.Create ToolTip and set text string as content