验证两个TextBox.Text值是不同的

时间:2013-06-09 13:50:02

标签: wpf xaml

抓住两个TextBox并说你需要验证他们的内容是不同的字符串。

示例:

  • 正确结果:prefix1,prefix2
  • 结果不正确:prefix1,prefix1

为了完成这项任务,我考虑过使用MultiBinding,但是出现了两个问题:

  • 应该放在哪里?目前它是在虚拟TextBox
  • 即使使用虚拟TextBox,也不会调用ValidationRule

不确定这种方法是否正确,你会怎么做?

<MultiBinding Converter="{StaticResource MyConverter}">
    <Binding Path="GradientPrefix"
                Source="{StaticResource Indices}"
                UpdateSourceTrigger="PropertyChanged" />
    <Binding Path="ColorPrefix"
                Source="{StaticResource Indices}"
                UpdateSourceTrigger="PropertyChanged" />
    <MultiBinding.ValidationRules>
        <gpl2Xaml:DistinctStringValidationRule />
    </MultiBinding.ValidationRules>
</MultiBinding>

1 个答案:

答案 0 :(得分:0)

以下是使用BindingGroup的解决方案!

BindingGroup级错误:

enter image description here

BindingGroup和字段级别出错:

enter image description here

没有错误:

enter image description here

以下是代码:

<Window>
    <Window.Resources>
        <gpl2Xaml:Indices x:Key="Indices"
                          ColorIndex="1"
                          ColorPrefix="MyColor"
                          GradientIndex="1"
                          GradientPrefix="MyColor" />
    </Window.Resources>
        <Grid DataContext="{StaticResource Indices}"
              Style="{StaticResource gridInError}"
              Validation.ErrorTemplate="{StaticResource validationTemplate}">
            <Grid.BindingGroup>
                <BindingGroup>
                    <BindingGroup.ValidationRules>
                        <gpl2Xaml:DistinctValidationRule />
                    </BindingGroup.ValidationRules>
                </BindingGroup>
            </Grid.BindingGroup>
            <TextBox x:Name="TextBoxGradientPrefix"
                     Style="{StaticResource textBoxInError}"
                     TextChanged="TextBoxGradientPrefix_OnTextChanged"
                     Validation.ErrorTemplate="{StaticResource validationTemplate}">
                <Binding Path="GradientPrefix"
                         Source="{StaticResource Indices}"
                         UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <gpl2Xaml:StringValidationRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox>
            <TextBox x:Name="TextBoxColorPrefix"
                     Style="{StaticResource textBoxInError}"
                     TextChanged="TextBoxColorPrefix_OnTextChanged"
                     Validation.ErrorTemplate="{StaticResource validationTemplate}">
                <Binding Path="ColorPrefix"
                         Source="{StaticResource Indices}"
                         UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <gpl2Xaml:StringValidationRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox>
        </Grid>
</Window>

每次触发验证的额外代码:

    private void TextBoxGradientPrefix_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        grid.BindingGroup.CommitEdit();
    }

    private void TextBoxColorPrefix_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        grid.BindingGroup.CommitEdit();
    }

验证规则:

public class DistinctValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        var bindingGroup = value as BindingGroup;
        if (bindingGroup == null) return new ValidationResult(false, "Not a BindingGroup");

        var o = bindingGroup.Items[0] as Indices;
        if (o == null) return new ValidationResult(false, "Not an Indices");

        if (o.ColorPrefix == o.GradientPrefix)
            return new ValidationResult(false, "Color prefix and Gradient prefix must be distinct.");

        return new ValidationResult(true, null);
    }
}