WPF C# Tutorial

    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF" Height="100" Width="280">
    
                    x:Key="textBoxInErrorStyle" 
            TargetType="{x:Type TextBox}" >
            
                
                                                                      RelativeSource.Self}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                
            

            
                
                    
                        
                            
                                                           ToolTip="{Binding Path=AdornedElement.ToolTip, RelativeSource={RelativeSource 
                                            Mode=FindAncestor,
                                            AncestorType={x:Type Adorner}}}"/>
                        
                    

                

            
        
    

    
        
        
            
              
                                            UpdateSourceTrigger="PropertyChanged" >
                       
                            
                        

                    
                

             
        

    

//File:Window.xaml.cs
using System.Globalization;
using System.Windows.Controls;
namespace WpfApplication1
{
    public class PercentageRule : ValidationRule
    {
        public override ValidationResult Validate(object value,CultureInfo cultureInfo)
        {
            string stringValue = value as string;
            if(!string.IsNullOrEmpty(stringValue))
            {
                double doubleValue;
                if(double.TryParse(stringValue, out doubleValue))
                {
                    if(doubleValue >= 0 && doubleValue <= 100)
                    {
                        return new ValidationResult(true, null);
                    }
                }
            }
            return new ValidationResult(false, "Must be a number between 0 and 100");
        }
    }
}