我对 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 示例?
谢谢你。米歇尔