双 NHibernate 类级别验证器问题

发布于 2024-08-06 04:46:47 字数 1357 浏览 4 评论 0原文

我正在使用 WPF,并且我有一个绑定到一系列控件的实体。该实体用两个类级别验证器装饰,如下所示:

[ExampleValidator1, ExampleValidator2]
public class Entity

实体具有一系列字段,其中并非所有字段都始终显示,具体取决于组合框中的选择。 每个选择都存在一个验证器,如果实体的“类型”与验证器返回 true 的特定验证器不匹配,显然正确的验证器将验证实际字段,如下所示:

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;
        }

XAML 如下:

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

显然我得到了漂亮的红色框和工具提示告知用户验证要求。

我的问题是,当组合框中的选择发生更改时,红色突出显示仍然存在(隐藏控件时会变成一个小红色方块)。 请有人指导我正确的方法!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-08-13 04:46:47

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

Solved by firing an OnPropertyChanged when the combobox is altered, not an ideal solution but its workable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文