询问者
ValidationRule中的参数绑定的问题

问题
-
目的:使用validationRule对用户输入的验证码进行验证;
方法:自定义了一个验证规则,比较用户输入与已经生成的验证码是否一致;
这个验证规则中有一个自定义的ToCompareObject类型dependencyObject,有一个data属性,想让这个data绑定到后台的 生成的验证码(如图图所示的)。
问题:这个data始终无法获取已经生成的验证码,一直是null,
我是新手,好几天都一头雾水,一筹莫展了。
我自定义的验证规则是:
public class CompareValidationRule : ValidationRule { public ToCompareObject CompareValue { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { if (value == null) { return new ValidationResult(false, "输入不正确"); } string v = value.ToString().Trim(); if (string.IsNullOrEmpty(v)) { return new ValidationResult(false, "输入不正确"); } if (v != CompareValue.Data) { return new ValidationResult(false, "输入不正确"); } return new ValidationResult(true, null); } } public class ToCompareObject : DependencyObject { public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data",typeof(String),typeof(ToCompareObject), null); public string Data { get { return (string)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } }
xaml中的代码:
<TextBox FontSize="14" Name="InputRandomCode" Width="130" MD:HintAssist.Hint="右侧验证码" > <TextBox.Text> <Binding Path="Code"> <Binding.ValidationRules> <v:CompareValidationRule > <v:CompareValidationRule.CompareValue> <v:ToCompareObject Data="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor}, Path=DataContext.RegisterRandomCode}" /> </v:CompareValidationRule.CompareValue> </v:CompareValidationRule> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox>
还有一个问题是:在visualStudio2019中,在ErrorList窗口中,没有显示报错。但在OutPut窗口中,显示有一个错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.
BindingExpression:Path=RegisterRandomCode; DataItem=null; target element is 'ToCompareObject' (HashCode=27077540);
target property is 'Data' (type 'String')
请教:哪里出了问题?
如果 对验证码,两个要进行比较验证,用什么方法更好?
非常感谢您的帮助!!
- 已编辑 MillionYearsAfter2020 2020年6月26日 10:43 修正问题
全部回复
-
应该还是自定义的那个类有问题:
绑定规则这样写时:
<v:ToCompareObject Data="{Binding ElementName=InputRandomCode, Path=Text}" />
显示的错误是:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for
target element. BindingExpression:Path=Text; DataItem=null; target element is 'ToCompareObject'
(HashCode=27077540); target property is 'Data' (type 'String')
绑定规则这样写时:
<v:ToCompareObject Data="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},
Path=DataContext.RegisterRandomCode}" />
显示的错误是:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor,
AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.RegisterRandomCode;
DataItem=null; target element is 'ToCompareObject' (HashCode=27077540);
target property is 'Data' (type 'String')
-
你好,
你提供的代码有部分缺失,比如没有Path="Code"中的Code部分,也没有RegisterRandomCode。你为什么这样使用DataContext.RegisterRandomCode? 还有你是有错误信息提示的,你的代码中没有看见具体的方式去显示它们。你能给我一个可以重现问题的demo去测试更快地分析问题吗?
谢谢
Daisy Tian
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.