gpt4 book ai didi

c# - 当值设置回有效值时,ValidationRules 不会消除错误

转载 作者:太空狗 更新时间:2023-10-29 23:47:35 24 4
gpt4 key购买 nike

简介

我已经创建了一个 DecimalTextBox UserControl,其中包含我需要完成的一些小数验证,这样我就不需要每次都重新创建验证,并且可以只使用 UserControl 代替。此验证具有需要绑定(bind)的属性,因此我创建了 DependencyProperties 以便我可以绑定(bind)到它们,根据 this article by Josh Smith .


问题

控件的验证行为异常。当我在 TextBox 中键入错误值时,它会显示为错误。但是,当我尝试将值更改回代码中时,文本框中显示的值保持不变。

以下是我执行的导致此错误的步骤(在此示例中,1 是无效值):

  1. 加载表单,默认值为0
  2. 在文本框中输入1(由于验证结果错误,文本框变为红色)
  3. 在代码中 我将绑定(bind)到文本框的属性设置为0
  4. 表单仍然在红色文本框中显示1

代码示例

我准备了一个演示问题的示例,可以下载here.

我会在这里发布一些代码,如果你想要更多,请告诉我。

ValidationTestControl 的 XAML

<UserControl x:Class="WPFTestProject.ValidationTestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:WPFTestProject"
x:Name="ValidationTest"
Height="50" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>

</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Type 'Banana' here: "></TextBlock>
<TextBox MinWidth="100">
<TextBox.Text>
<Binding ElementName="ValidationTest" Path="Text" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
<Binding.ValidationRules>
<v:NotBananaValidationRule>
<v:NotBananaValidationRule.NotWhatBinding>
<v:NotBananaBinding x:Name="NotBananaValidationBinding"></v:NotBananaBinding>
</v:NotBananaValidationRule.NotWhatBinding>
</v:NotBananaValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock Text=" (the text will give error when = 'Banana')"></TextBlock>
</StackPanel>
</Grid>

ValidationTestControls 代码隐藏

(是的,我不太了解 MVVM,但我觉得这个独立控件没问题)

 public partial class ValidationTestControl : UserControl
{
public ValidationTestControl()
{
InitializeComponent();
Banana = "Banana";

Binding BananaBinding = new Binding("Banana");
BananaBinding.Source = this;

NotBananaValidationBinding.SetBinding(NotBananaBinding.NotWhatProperty, BananaBinding);
}

public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ValidationTestControl), new PropertyMetadata());
public static DependencyProperty BananaProperty = DependencyProperty.Register("Banana", typeof(string), typeof(ValidationTestControl), new PropertyMetadata());

public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}


public string Banana
{
get
{
return (string)GetValue(BananaProperty);
}
set
{
SetValue(BananaProperty, value);
}
}


}

为绑定(bind)创建的 ValidationRule 和 FrameWorkElement

 public class NotBananaValidationRule:ValidationRule
{
private NotBananaBinding _notWhatBinding;
public NotBananaBinding NotWhatBinding
{
get { return _notWhatBinding; }
set { _notWhatBinding = value; }
}

public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string what = value.ToString();

if(what == _notWhatBinding.NotWhat||string.IsNullOrEmpty(what))
return new ValidationResult(false,
"Please enter a string that is not " + _notWhatBinding.NotWhat);
else
return new ValidationResult(true, null);

}

}


public class NotBananaBinding : FrameworkElement
{
public static readonly DependencyProperty NotWhatProperty = DependencyProperty.Register(
"NotWhat", typeof(string), typeof(NotBananaBinding), new UIPropertyMetadata());

public string NotWhat
{
get { return (string)GetValue(NotWhatProperty); }
set { SetValue(NotWhatProperty, value); }
}

public NotBananaBinding() { }
}

基本上这段代码的作用是检查您是否输入了“Banana”,然后返回验证错误。该控件公开了依赖属性,因为我希望在使用该控件时能够绑定(bind)到它们。 FrameworkElement NotBananaBinding 让我创建依赖属性(因为它是一个 DependencyObject 所以我可以为验证绑定(bind)东西。ValidationRule 有一个 NotBananaBinding 存储依赖属性并在验证方法中使用它的属性。

我知道我的属性名称有点糟糕,抱歉。问题是该示例在显示错误方面做得很好。在我匆忙举个例子时,我没有很好地命名变量。如果您发现代码糟糕请下载示例 here.


到目前为止我想出了什么

基本上这个问题似乎是因为我实际上没有改变值

即使我在属性上调用 OnPropertyChanged,因为值没有不同,它也不会尝试重新评估验证。

我显然可以将该值更改为某个任意有效值,然后将其更改为我想要的值并且它会起作用,但我希望有某种方法可以手动进行调用验证,重新评估该值并然后改变它等等。改变它来回有点困惑。


结论

我是不是做错了什么(也许是关于我从 Josh Smiths 的帖子中实现验证和绑定(bind)的方式)

这只是一个 C# 错误,还是有意为之?如果是,那为什么?

有什么优雅的方法可以修复它吗?

你_你

最佳答案

验证会阻止设置 Text 属性。在 setter 上放置一个断点,您将看到当您键入最后一个 'a' 时它不会中断。如果您键入 Bananan 并按下退格键,但出现错误,请按下按钮,它就会工作。验证可确保您的属性中没有无效值。因此,如果您在出错时将其保存到比方说数据库,它不会保存无效值。

关于c# - 当值设置回有效值时,ValidationRules 不会消除错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com