我是WPF的新手。我试图将验证规则应用于两个文本框,以便比较它们的值是否相同。我遇到了 BindingGroups 并决定尝试一下。但是现在,我发现我的验证规则没有被触发。你能告诉我我在这里失踪了吗?
这是我的代码:
XAML:
<UserControl>
<UserControl.BindingGroup>
<BindingGroup Name="FormBindingGroup">
<BindingGroup.ValidationRules>
<local: confirmemailvalidator ValidationStep="ConvertedProposedValue"/>
</BindingGroup.ValidationRules>
</BindingGroup>
</UserControl.BindingGroup>
<Dockpanel>
<Grid>
<Grid>
<Label Content="Enter email"/>
<TextBox>
<TextBox.Text>
<Binding Path="email" BindingGroupName="FormBindingGroup"/>
</TextBox.Text>
</TextBox>
</Grid>
<Grid>
<Label Content="Confirm email"/>
<TextBox>
<TextBox.Text>
<Binding Path="confirmEmail" BindingGroupName="FormBindingGroup"/>
</TextBox.Text>
</TextBox>
</Grid>
</Grid>
</DockPanel>
</UserControl>
验证码
class confirmemailvalidator: ValidationRule
{
public override ValidationResult Validate
(objectvalue,CultureInfo cultureInfo)
{
ValidationResult result = new ValidationResult(true, null);
BindingGroup bindingGroup = (BindingGroup)value;
string originalEmail = bindingGroup.Items[0].ToString();
string confirmEmail = bindingGroup.Items[1].ToString();
if (!originalEmail.Equals(confirmEmail))
{
result = new ValidationResult(false, this.ErrorMessage);
}
return result ;
}
}
}
我已经使用ValidationRules为单个文本框(检查输入格式)实现了验证,并且它工作正常。为什么不会触发此BindingGroup的Validate方法? :(