我已经搜索了所有的谷歌,并仍然试图找到正确的答案。问题如下:当我清除这个文本字段时,绑定值不会被触发。
因此,问题是,如果该文本字段为空,则该字段的有界值不会被更改,但我的验证规则确实检测到它并发出警告。
在下面,您可以找到我的XAML片段、归属验证规则和上述属性。
<:StandardValidationRule ValidatesOnTargetUpdated="True"/>
公共类StandardValidationRule : ValidationRule {公共覆盖ValidationResult value (object value,CultureInfo cultureInfo) { var valueToValidate =值作为字符串;if (string.IsNullOrEmpty(valueToValidate)) {返回新ValidationResult(false,“字段是强制性的。”);}返回新ValidationResult(true,null);}
私有字符串_additionalSurfaceTreatmentInfo;公共字符串AdditionalSurfaceTreatmentInfo { get => _additionalSurfaceTreatmentInfo;set { _additionalSurfaceTreatmentInfo = value;OnPropertyChanged();SetOrModifySurfaceTreatment();SetOrModifySurfaceTreatment }
提前感谢你的努力。任何帮助都是非常感谢的!
上面的代码可以像我喜欢的那样工作。我已经尝试了关于我可以在ValidationRule中填充的不同属性的一切。唯一需要更改的是,当textbox为空时,它必须触发OnPropertyChanged()方法。这样,以后当我提交一个保存命令时,我就可以验证该属性。
发布于 2022-11-02 17:56:55
使用ValidationStep属性。示例:
<local:StandardValidationRule ValidationStep="UpdatedValue"
ValidatesOnTargetUpdated="True"/>
但是要验证源属性,需要更复杂的逻辑,因为验证器不接收属性值,而是从目标属性接收绑定表达式。
public class StandardValidationRule : ValidationRule
{
private bool gettingValue = false;
private bool isValueReceived = false;
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (gettingValue)
{
isValueReceived = true;
return ValidationResult.ValidResult;
}
string? valueToValidate = value as string;
ValidationResult? result = null;
if (valueToValidate is null && value is not null)
{
if (value is BindingExpressionBase bindingExpression)
{
gettingValue = true;
isValueReceived = false;
DependencyObject target = bindingExpression.Target;
var gettingValueExpression = BindingOperations.SetBinding(target, SourceValueProperty, bindingExpression.ParentBindingBase);
if (!isValueReceived)
{
gettingValueExpression.UpdateTarget();
}
valueToValidate = target.GetValue(SourceValueProperty)?.ToString();
target.ClearValue(SourceValueProperty);
gettingValue = false;
}
else
{
result = unvalid;
}
}
if (result is null)
{
result = string.IsNullOrEmpty(valueToValidate)
? unvalid
: ValidationResult.ValidResult;
}
return result;
}
private static readonly ValidationResult unvalid = new ValidationResult(false, "Field is mandatory.");
// Using a DependencyProperty as the backing store for SourceValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SourceValueProperty =
DependencyProperty.RegisterAttached("SourceValue", typeof(object), typeof(StandardValidationRule), new PropertyMetadata(null));
}
还有另一种获取源值的方法。
它的基础是通过反射使用internal
方法。
许多人认为这是一种糟糕的方式。
但它的效率要高得多。
我认为不太可能有人会对已经在许多地方使用的internal
方法进行更改。
public static class BindingExpressionHelper
{
private static readonly Func<BindingExpressionBase, DependencyObject, DependencyProperty, object> GetValueOfBindingExpression;
static BindingExpressionHelper()
{
Type beType = typeof(BindingExpressionBase);
var beMethod = beType
.GetMethod("GetValue", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, new Type[] { typeof(DependencyObject), typeof(DependencyProperty) })
?? throw new Exception("GetValue method not found.");
var beFunc = (Func<BindingExpressionBase, DependencyObject, DependencyProperty, object>)
beMethod.CreateDelegate(typeof(Func<BindingExpressionBase, DependencyObject, DependencyProperty, object>));
GetValueOfBindingExpression = beFunc;
}
/// <summary>Returns the source value of this binding expression.</summary>
/// <param name="bindingExpression">The binding expression whose value to get.</param>
/// <returns>The value of the binding expression received.</returns>
public static object? GetSourceValue(this BindingExpressionBase bindingExpression)
{
DependencyObject target = bindingExpression.Target;
DependencyProperty targetProperty = bindingExpression.TargetProperty;
var value = GetValueOfBindingExpression(bindingExpression, target, targetProperty);
return value;
}
}
public class StandardValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string? valueToValidate = value as string;
ValidationResult? result = null;
if (valueToValidate is null && value is not null)
{
if (value is BindingExpressionBase bindingExpression)
{
valueToValidate = bindingExpression.GetSourceValue()?.ToString();
}
else
{
result = unvalid;
}
}
if (result is null)
{
result = string.IsNullOrEmpty(valueToValidate)
? unvalid
: ValidationResult.ValidResult;
}
return result;
}
private static readonly ValidationResult unvalid = new ValidationResult(false, "Field is mandatory.");
}
https://stackoverflow.com/questions/74293130
复制相似问题