wpf ValidationRule数据验证
2023-09-14 09:16:30 时间
先写验证规则代码
public class RangeValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
Double D = 0;
if (double.TryParse(value.ToString(), out D))
{
if (D > 0 && D < 100)
{
return new ValidationResult(true,null);//验证成功
}
}
return new ValidationResult(false, "Validation Failed");//验证失败
}
}
xaml界面
<Window x:Class="WpfApp1.Window8"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:Validatarolus="clr-namespace:WpfApp1.ValidationRules"
mc:Ignorable="d"
Title="Window8" Height="450" Width="800">
<Grid x:Name="grid">
<StackPanel>
<Slider x:Name="slider" Maximum="1000"></Slider>
<TextBox Validation.Error="TextBox_Error">
<TextBox.Text>
<Binding ElementName="slider" Path="Value" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
<Binding.ValidationRules>
<Validatarolus:RangeValidationRule ValidatesOnTargetUpdated="True"></Validatarolus:RangeValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Button Content="提交" Click="Button_Click"></Button>
</StackPanel>
</Grid>
</Window>
public partial class Window8 : Window
{
public Window8()
{
InitializeComponent();
}
private void TextBox_Error(object sender, ValidationErrorEventArgs e)
{
if (Validation.GetErrors(sender as FrameworkElement).Count > 0)
{
(sender as FrameworkElement).ToolTip =Validation.GetErrors(sender as FrameworkElement)[0].ErrorContent.ToString();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string message;
if (HasErrors(out message))
{
MessageBox.Show(message);
}
}
private bool HasErrors(out string message)
{
StringBuilder sb = new StringBuilder();
GetError(sb, this.grid);
message = sb.ToString();
return !string.IsNullOrWhiteSpace(message);
}
private void GetError(StringBuilder sb, DependencyObject obj)
{
foreach (object child in LogicalTreeHelper.GetChildren(obj))
{
StackPanel element = child as StackPanel;
if (element == null) continue;
foreach (var ele in element.Children)
{
TextBox textBox = ele as TextBox;
if (textBox == null) continue;
if (Validation.GetHasError(textBox))
{
foreach (ValidationError error in Validation.GetErrors(textBox))
{
sb.Append(error.ErrorContent.ToString() + "\r\n");
}
GetError(sb, element);
}
}
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/bb449de011fa3e85116bdc6c68dff365.png)
相关文章
- WPF中使用WPFMediaKit视频截图案例
- WPF第三方控件盘点
- 分享一个很好看的WPF界面
- 关于开发WPF的一些感想
- 如何让WPF程序用上MVVM模式
- WPF 看板组件:Kanban Library for WPF 4.2 Crack
- WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)
- WPF中DataGrid CheckBox选择数据错乱
- wpf仪表盘
- WPF 自定义TreeView控件样式,仿QQ联系人列表
- WPF中XAML中使用String.Format格式化字符串示例
- wpf datagridrowstyle
- WPF、Windows Forms和Silverlight间的联系和区别(转)
- WPF绘制简单常用的Path(转)
- WPF material UI 的使用
- C# WPF 后台调整怎样使用代码把指定控件的Z序调整到最前面呢?
- WPF设计自定义控件
- WPF MVVM从入门到精通8:数据验证
- MVVM 模式下 WPF Password 控件的数据绑定解析
- WPF绑定(Binding)下的数据验证IDataErrorInfo