ToolTip With Binding : ToolTip « Windows Presentation Foundation « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Windows Presentation Foundation » ToolTipScreenshots 
ToolTip With Binding
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)[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) {
      ifthis.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) {
      iftargetType != 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");
      }

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

   
    
  
Related examples in the same category
1.ToolTip for BorderToolTip for Border
2.Using ToolTip for TextBox with TextBox.ToolTip and ToolTip tagUsing ToolTip for TextBox with TextBox.ToolTip and ToolTip tag
3.Add ToolTip text to a TextBoxAdd ToolTip text to a TextBox
4.ToolTipService.InitialShowDelayToolTipService.InitialShowDelay
5.ToolTip with ImageToolTip with Image
6.ToolTipService.Placement="Bottom"ToolTipService.Placement=
7.ToolTip with List itemsToolTip with List items
8.A Tool Tip on a Disabled ControlA Tool Tip on a Disabled Control
9.Disabled Button with ToolTipServiceDisabled Button with ToolTipService
10.Control the Display Duration and Position of a Tool TipControl the Display Duration and Position of a Tool Tip
11.Set ToolTip Placement, ShowDuration, VerticalOffsetSet ToolTip Placement, ShowDuration, VerticalOffset
12.Set ToolTipService.Placement="Center"Set ToolTipService.Placement=
13.Set drop shadow for a ToolTipSet drop shadow for a ToolTip
14.Add Image to ToolTipAdd Image to ToolTip
15.Set border for ToolTip by using ControlTemplateSet border for ToolTip by using ControlTemplate
16.Complex ToolTipComplex ToolTip
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.