c# - WPF 文本框验证问题
问题描述
我有文本框,这些名称是 checkDescriptionTxtBox 和 parameterTxtBox。两者都使用相同的验证规则,例如在 TextBoxValidator.cs 中可用。但一个区别是在 parameterTxtBox 中添加了 ValidationStep="UpdatedValue"。checkDescriptionTxtBox 验证按预期工作,但为什么 parameterTxtBox 验证不起作用?你能帮助任何人吗?
我想在源属性更新后验证值。我已经调试了 Validate 方法。在 checkDescriptionTxtBox 验证参数值作为字符串传递但当 parameterTxtBox 验证参数值不作为字符串传递而是差异绑定值时。所以我想在源属性更新后验证值。如何做到这一点?
<TextBox Name="checkDescriptionTxtBox" Width="700" TextWrapping="Wrap" AcceptsReturn="True" ScrollViewer.VerticalScrollBarVisibility="Auto" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}">
<TextBox.Text>
<Binding Path="CheckDescription" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<v:TextBoxValidator></v:TextBoxValidator>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Name="parameterTxtBox" Margin="10,0,0,0" Width="200" Height="20" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}">
<TextBox.Text>
<Binding Path="ParameterValue" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<v:TextBoxValidator ValidationStep="UpdatedValue"></v:TextBoxValidator>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
TextBoxValidator.cs
public class TextBoxValidator : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (String.IsNullOrEmpty(value.ToString().Trim()))
return new ValidationResult(false, "Value cannot be empty");
return ValidationResult.ValidResult;
}
}
解决方案
将ValidationStep
属性设置为UpdatedValue
意味着验证规则将在源属性更新后运行。如果源属性未更新,验证规则将不会运行。该ValidationStep
属性的默认值是RawProposedValue
表示验证规则在值转换发生之前运行。
因此,由于checkDescriptionTxtBox
有效,您应该简单地从ValidationStep="UpdatedValue"
ofTextBoxValidator
中删除parameterTxtBox
。
但我想在源属性更新后验证值。我已经调试了 Validate 方法。在 checkDescriptionTxtBox 验证参数值作为字符串传递但当 parameterTxtBox 验证参数值不作为字符串传递而不是差异绑定值时。所以我想在源属性更新后验证值。如何做到这一点?
使用value
一些BindingExpression
反射:
public class TextBoxValidator : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string s = null;
BindingExpression be = value as BindingExpression;
if(be != null)
{
object sourceObject = be.ResolvedSource;
string sourceProperty = be.ResolvedSourcePropertyName;
if(sourceObject != null && sourceProperty != null)
{
PropertyInfo pi = sourceObject.GetType().GetProperty(sourceProperty);
s = pi.GetValue(sourceObject) as string;
}
}
else
{
s = value as string;
}
if (string.IsNullOrEmpty(s))
return new ValidationResult(false, "Value cannot be empty");
return ValidationResult.ValidResult;
}
}
目前我正在使用 .Net 4 。我希望是.ResolvedSource; 和 .Net 4.5 中支持的 be.ResolvedSourcePropertyName。那么我如何在.Net 4中实现这一点?
这应该适用于 .NET Framework 4:
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string s = null;
BindingExpression be = value as BindingExpression;
if (be != null)
{
object sourceObject = be.DataItem;
string sourceProperty = be.ParentBinding.Path.Path;
if (sourceObject != null && sourceProperty != null)
{
PropertyInfo pi = sourceObject.GetType().GetProperty(sourceProperty);
s = pi.GetValue(sourceObject) as string;
}
}
else
{
s = value as string;
}
if (string.IsNullOrEmpty(s))
return new ValidationResult(false, "Value cannot be empty");
return ValidationResult.ValidResult;
}
推荐阅读
- javascript - 在我将 angualr 5 更新为 7 之后,同时在 angular 1.x 中运行 angular 5 时,无法实例化模块 $$UpgradeModule?
- magento-1.9 - magento 1.9 版 Transactional Email 模板内容大小限制
- node.js - 如何在对话流中制作水平轮播?
- in-app-purchase - 实施自动更新订阅
- java - 如何使用可分页和对象创建缓存键?
- swift - Swift:在 Mac 上检索文件图标时出现问题
- flutter - 如何检查用户名是否已存在于数据库中并验证没有空格的用户名
- javascript - 如何让用户的 gmail 使用我的应用程序脚本?
- scala - 如何读取带有分隔符 /u0001 的 .dat 文件并记录下一条记录将通过 scala 中的下一行分隔
- angular - 日期的角度自定义管道错误作为字符串