WPF:在两个组合框上同时运行验证器 [英] WPF: Run Validator on two comboboxes as the same time

查看:67
本文介绍了WPF:在两个组合框上同时运行验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个组合框,其中包含相似的项目列表。这两个组合框不能具有相同的值。

I have the following two comboboxes that contain similar list of items. The two comboboxes cannot have the same value.

            <ComboBox Name="OldEmpNoListBox"
                      IsReadOnly="True"
                      ItemsSource="{Binding OldEmpNoCollection}"
                      SelectedValue="{Binding Model.FieldNameForOldEmpNo, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                      HorizontalAlignment="Left"
                      Width="150"
                      IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}"
                      Grid.Row="1" Grid.Column="1">
                <ComboBox.Text>
                    <Binding Path="Model" 
                             UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <vm:FieldNamesValidator ValidatesOnTargetUpdated="True" />
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.Text>
            </ComboBox>

            <ComboBox Name="OldCompanyListBox"
                      IsReadOnly="True"
                      ItemsSource="{Binding OldCompanyCollection}"
                      SelectedValue="{Binding Model.FieldNameForOldCompany, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                      HorizontalAlignment="Left"
                      Width="150"
                      Grid.Row="2" Grid.Column="1">
                <ComboBox.Text>
                    <Binding Path="Model" 
                             UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <vm:FieldNamesValidator ValidatesOnTargetUpdated="True" />
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.Text>
            </ComboBox>

这里是验证者:

/// <summary>
/// Duplicate Values Validator
/// </summary>
public class FieldNamesValidator : ValidationRule
{
    /// <summary>
    /// Validate OldEmpNo field name does not match Old Company field name
    /// </summary>
    /// <param name="value"></param>
    /// <param name="cultureInfo"></param>
    /// <returns></returns>
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        EmpNoOptionsSettingsModel _empNoOptions = (App.Current.Resources["Locator"] as ViewModelLocator).ParametersEmpNoOptionsViewModel.Model;

        // if OldEmpNo field name = Old Company field name
        if (_empNoOptions.FieldNameForOldEmpNo == _empNoOptions.FieldNameForOldCompany)
        {
            return new ValidationResult(false, "Field name for Old Emp No cannot be the same as the field name for Old Company");
        }           

        return ValidationResult.ValidResult;
    }
}

验证工作与预期的一样,只是它仅在当前正在更改的任何组合框。例如,如果组合框具有相同的值,则每个框都以红色框出轮廓。我更改了第一个框,红色被删除,但红色保留在第二个框上。

The validation works as expected except that it only operates on whichever combobox is currently being changed. For instance, if the comboboxes have the same value, each box is outlined in red. I change the first box and the red is removed, but the red remains on the second box. Is there a way to get both comboboxes to validate themselves anytime one of them is changed?

推荐答案


在绑定源属性
更改事件时触发验证规则。

Validation rule gets fired on binding source property changed event.

更改绑定中的属性名称以指向实际属性:

Change property name in binding to point to actual property:

<ComboBox.Text>
    <Binding Path="Model.FieldNameForOldEmpNo" 
             UpdateSourceTrigger="PropertyChanged">
        .....
</ComboBox.Text>

<ComboBox.Text>
    <Binding Path="Model.FieldNameForOldCompany" 
             UpdateSourceTrigger="PropertyChanged">
        .....
</ComboBox.Text>

然后从这两个属性的设定者中,提高其他财产的财产变更,以使其相应验证规则被解雇。

And from setter of both of these properties, raise property change for other property as well so that its corresponding validation rule gets fired.

这篇关于WPF:在两个组合框上同时运行验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆