一。校验
一般需要对target上的值进行校验。
xaml:
1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Simple Binding" Height="135" Width="300"> 5 <StackPanel x:Name="stackPanel" Background="LightBlue"> 6 <TextBox x:Name="textBox1" Margin="5" /> 7 <Slider x:Name="slider1" Minimum="0" Maximum="100" Margin="5" /> 8 </StackPanel> 9 </Window>
写一个RangeValidationRule类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Controls; 7 8 namespace WpfApplication1 9 { 10 class RangeValidationRule : ValidationRule 11 { 12 public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) 13 { 14 double d = 0; 15 if (double.TryParse(value.ToString(), out d)) 16 { 17 if (d >= 0 && d <= 100) 18 { 19 return new ValidationResult(true, null); 20 } 21 } 22 return new ValidationResult(false, "Validation Failed"); 23 } 24 } 25 }
再在xaml.cs里这么写
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 using System.Data; 16 using MySql.Data; 17 using MySql.Data.Entity; 18 using MySql.Data.MySqlClient; 19 using System.Xml; 20 using System.Xml.Linq; 21 22 namespace WpfApplication1 23 { 24 /// <summary> 25 /// Interaction logic for MainWindow.xaml 26 /// </summary> 27 public partial class MainWindow : Window 28 { 29 public MainWindow() 30 { 31 InitializeComponent(); 32 Binding binding = new Binding("Value") { Source = this.slider1 }; 33 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 34 RangeValidationRule rvr = new RangeValidationRule(); 35 binding.ValidationRules.Add(rvr); 36 this.textBox1.SetBinding(TextBox.TextProperty, binding); 37 } 38 39 } 40 }
如果在textbox里的数不在0到100范围里,其边框就会变成红色
在这里Source是slider1,target是textBox1。一般来说Source的数据都是正确的,而target的数据可能是用户输入的,有可能是不正确的,所以需要校验。上面的代码是不会对Source更新数据时进行校验的,如果说Source的数据也有可能有问题,我们就需要将校验条件的ValidatesOnTargetUpdated属性设为true
xaml:
1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Simple Binding" Height="135" Width="300"> 5 <StackPanel x:Name="stackPanel" Background="LightBlue"> 6 <TextBox x:Name="textBox1" Margin="5" /> 7 <Slider x:Name="slider1" Minimum="-10" Maximum="110" Margin="5" /> 8 </StackPanel> 9 </Window>
xaml.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 using System.Data; 16 using MySql.Data; 17 using MySql.Data.Entity; 18 using MySql.Data.MySqlClient; 19 using System.Xml; 20 using System.Xml.Linq; 21 22 namespace WpfApplication1 23 { 24 /// <summary> 25 /// Interaction logic for MainWindow.xaml 26 /// </summary> 27 public partial class MainWindow : Window 28 { 29 public MainWindow() 30 { 31 InitializeComponent(); 32 Binding binding = new Binding("Value") { Source = this.slider1 }; 33 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 34 RangeValidationRule rvr = new RangeValidationRule(); 35 rvr.ValidatesOnTargetUpdated = true; 36 binding.ValidationRules.Add(rvr); 37 this.textBox1.SetBinding(TextBox.TextProperty, binding); 38 } 39 40 } 41 }
当slider的值小于0或者大于100时,textbox的边框就会变红,数字是不在0到100内的
如果要显示这个错误信息,需要加入侦听器,具体代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 using System.Data; 16 using MySql.Data; 17 using MySql.Data.Entity; 18 using MySql.Data.MySqlClient; 19 using System.Xml; 20 using System.Xml.Linq; 21 22 namespace WpfApplication1 23 { 24 /// <summary> 25 /// Interaction logic for MainWindow.xaml 26 /// </summary> 27 public partial class MainWindow : Window 28 { 29 public MainWindow() 30 { 31 InitializeComponent(); 32 Binding binding = new Binding("Value") { Source = this.slider1 }; 33 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 34 RangeValidationRule rvr = new RangeValidationRule(); 35 rvr.ValidatesOnTargetUpdated = true; 36 binding.ValidationRules.Add(rvr); 37 binding.NotifyOnValidationError = true; 38 this.textBox1.SetBinding(TextBox.TextProperty, binding); 39 this.textBox1.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError)); 40 } 41 42 private void ValidationError(object sender, RoutedEventArgs e) 43 { 44 if (Validation.GetErrors(this.textBox1).Count > 0) 45 { 46 this.textBox1.ToolTip = Validation.GetErrors(this.textBox1)[0].ErrorContent.ToString(); 47 } 48 } 49 } 50 }
二。 转换
时间: 2024-10-06 03:59:37