开发者

WPF, EntLib: PropertyComparisonValidator does not update the UI of the compared value

开发者 https://www.devze.com 2023-01-29 16:54 出处:网络
I have a problem with the PropertyComparisonValidator of EntLib 5.0. I set up an simple form with an Min and a Max field.

I have a problem with the PropertyComparisonValidator of EntLib 5.0. I set up an simple form with an Min and a Max field. The validation is: when Min >= Max both properties are invalid.

    [RangeValidator(10, RangeBoundaryType.Inclusive, 100,
        RangeBoundaryType.Inclusive)]
    [PropertyCompa开发者_开发问答risonValidator("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); }
    }

The 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}"/>

The problem is the UI. If I input Min=5 and Max=4 then both are invalid and marked with a red border.

BUT if I update Min=3 --> both shold be correct. Checking the validation it returns NO ERROR and is perfect. -> But the UI still remains red for Max. Only Min will be updated, because this field had a PropertyChanged.

Is there a working Min Max example with EntLib for WPF?

Thank you. Michele


Unfortunately, there is a design flaw in VAB 5.0 concerning the PropertyComparisonValidator. You can't decorate your objects with this validator when using one of the integration libraries (as you do for WPF). This is unfortunate, because I think that using a PropertyComparisonValidator is much cleaner than writing this in a [SelfValidation] method.

I created a discussion about this on the VAB forum. I hope the EntLib team will fix this in the next release.

In the meantime: don't use the PropertyComparisonValidator and write these validations in the [SelfValidation] method of a type.

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号