点滴积累,融会贯通

-----喜欢一切有兴趣的东西

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

XAML

<Window x:Class="WpfApplication1.Window1"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Title
="Window1" Height="192" Width="537"
    xmlns:local
="clr-namespace:WpfApplication1">

    
<Window.Resources>
        
<!--判断规则-->
        
<local:NumberRangeRule x:Key="numberRangeRule"></local:NumberRangeRule>
    
</Window.Resources>

    
<Grid Name="grid" ClipToBounds="False"  Height="129" Width="297">
        
<Label HorizontalAlignment="Left" Margin="25,53,0,0" Name="label2" Width="49" Height="28" VerticalAlignment="Top">年龄:</Label>
        
<!--绑定 Error Content 到ToolTip-->
        
<TextBox Margin="94,50,0,56"   ToolTip="{Binding RelativeSource={RelativeSource self}, Path=(Validation.Errors)[0].ErrorContent}" 
                 Name
="txtAge" HorizontalAlignment="Left" Width="101">
            
<TextBox.Text>
                
<!--绑定Age数据 和 判断规则 并设置最大最小值-->
                
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged">
                    
<Binding.ValidationRules>
                        
<local:NumberRangeRule Min="0" Max="128" />
                    
</Binding.ValidationRules>
                
</Binding>
            
</TextBox.Text>
        
</TextBox>
    
</Grid>
</Window>

 

 

C#

using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;

namespace WpfApplication1
{
    
/// <summary>
    
/// Window1.xaml 的交互逻辑
    
/// </summary>
    public partial class Window1 : Window
    {
        
public Person person;
        
public Window1()
        {
            InitializeComponent();

            
//创建示例数据并绑定
            person = new Person();
            person.Name 
= "kill";
            person.Age 
= 21;
            
this.txtAge.DataContext = person;
        }
    }

    
//数据类要继承INotifyPropertyChanged
    public class Person : INotifyPropertyChanged
    {
        
public event PropertyChangedEventHandler PropertyChanged;

        
string name;
        
public string Name
        {
            
get { return name; }
            
set { name = value; }
        }

        
int age;
        
public int Age
        {
            
get { return age; }
            
set { age = value; }
        }

        
public Person() { }   
    }

    
//书写继承ValidationRule的判断规则类
    
//重写Validate方法完成自己的判断
    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)
            {
                
return new ValidationResult(falsestring.Format("Number out of range ({0} - {1})", min, max));
            }

            
return ValidationResult.ValidResult;
        }
    }

}

 

 

posted on 2010-07-26 09:50  小寒  阅读(4219)  评论(2编辑  收藏  举报