0

拿两个 TextBox 并说您需要验证它们的内容是不同的字符串。

例子 :

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

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

  • 它应该放在哪里?目前它在一个虚拟的 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>
4

1 回答 1

1

这是使用 BindingGroup 的解决方案!

BindingGroup 级别的错误:

在此处输入图像描述

BindingGroup 和字段级别的错误:

在此处输入图像描述

没有错误:

在此处输入图像描述

这是代码:

<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);
    }
}
于 2013-06-09T16:13:24.143 回答