简体   繁体   中英

Dual NHibernate Class Level Validators Issue

I'm using WPF and I've got an Entity bound to a series of controls. The entity is decorated with two class level validators as follows:

[ExampleValidator1, ExampleValidator2]
public class Entity

An Entity has a series of fields of which not all are always shown, dependent on the selection from combo box. A validator exists for each of these selections, if the "type" of entity does not match a particular validator that validator returns true and obviously the correct validator will validate the actual fields as follows:

public bool IsValid(object value, IConstraintValidatorContext constraintValidatorContext)
    {

        constraintValidatorContext.DisableDefaultError();
        var allPropertiesValid = true;
        var entity= (Entity)value;

        if (supplier.ParticularEntityType)
        {
            return true;
        }



        if (String.IsNullOrEmpty(entity.Name) || entity.Name.Length > 50)
        {
            constraintValidatorContext.AddInvalid<Entity, string>("must be 50 or shorter and not empty", x => x.Name);
            allPropertiesValid = false;
        }

and the XAML is as follows:

                <TextBox Grid.Row="0" Grid.Column="3">
                    <TextBox.Text>
                        <Binding Path="Entity.Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                        </Binding>
                    </TextBox.Text>
                </TextBox>

Obviously I get the nice pretty red box and tool tips informing users of the validation requirements.

My issue is that when the selection in the combobox is changed, the red highlighting persists (becomes a small red square when a control is hidden). Could someone direct me the right way please!

通过更改组合框时触发OnPropertyChanged来解决,这不是理想的解决方案,但可行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM