1

我对 EntLib 5.0 的 PropertyComparisonValidator 有疑问。我设置了一个带有 Min 和 Max 字段的简单表单。验证是:当 Min >= Max 时,两个属性都无效。

    [RangeValidator(10, RangeBoundaryType.Inclusive, 100,
        RangeBoundaryType.Inclusive)]
    [PropertyComparisonValidator("MinVal", ComparisonOperator.GreaterThanEqual,
        MessageTemplate = @"Min cannot be greater or equal to Max")]
    [Required(ErrorMessage = @"MaxVal is required")]
    public int MaxVal
    {
        get { return (int)this.GetValue(MaxValProperty); }
        set { this.SetValue(MaxValProperty, value); }
    }

    [RangeValidator(1, RangeBoundaryType.Inclusive, 100,
        RangeBoundaryType.Inclusive)]
    [PropertyComparisonValidator("MaxVal", ComparisonOperator.LessThanEqual,
        MessageTemplate = @"Max cannot be less or equal to Min")]
    [Required(ErrorMessage = @"MinVal is required")]
    public int MinVal
    {
        get { return (int)this.GetValue(MinValProperty); }
        set { this.SetValue(MinValProperty, value); }
    }

XAML:

    <TextBox x:Name="txtMinVal" Margin="0,0,5,0" TextWrapping="Wrap" Text="{Binding MinVal, ValidatesOnDataErrors=true, NotifyOnValidationError=true, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" Grid.Row="1"
    />

    <Label x:Name="lblMinVal" Content="Min Value" Margin="5,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Row="1"/>

    <TextBox x:Name="txtMaxVal" Margin="0,0,5,0" TextWrapping="Wrap" Text="{Binding MaxVal, ValidatesOnDataErrors=true, NotifyOnValidationError=true, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" Grid.Row="2"
    />
    <Label x:Name="lblMaxVal" Content="Max Value" Margin="5,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Row="2"/>

    <Button x:Name="btnSave" Content="Save" Margin="0" Grid.Row="3" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" IsEnabled="{Binding IsValid}"/>

问题是用户界面。如果我输入 Min=5 和 Max=4 则两者都无效并标有红色边框。

但是如果我更新 Min=3 --> 两者都应该是正确的。检查验证它返回 NO ERROR 并且是完美的。-> 但是 Max 的 UI 仍然是红色的。只有 Min 会被更新,因为这个字段有一个 PropertyChanged。

EntLib for WPF 是否有一个有效的 Min Max 示例?

谢谢你。米歇尔

4

1 回答 1

1

不幸的是,VAB 5.0 中存在一个设计缺陷,涉及PropertyComparisonValidator. 在使用其中一个集成库时(就像对 WPF 所做的那样),您不能使用此验证器来装饰您的对象。这很不幸,因为我认为使用 a比在方法PropertyComparisonValidator中编写它要干净得多。[SelfValidation]

我在VAB 论坛上对此进行了讨论。我希望 EntLib 团队将在下一个版本中解决这个问题。

同时:不要在类型的方法中使用PropertyComparisonValidator和编写这些验证。[SelfValidation]

我希望这有帮助。

于 2010-12-12T14:10:18.427 回答