wpf targetting version 4.0 from 3.5 affects IDataErrorInfo implementation
I've an MVVM application implementing IDataErrorInfo using version 3.5. I want to try and target 4.0 so have amended the Target Framework setting and changed a few bits around (BitmapFrames and the like). Most things seem just fine and the process was relatively painless, until I noticed that the implentation of IDataErrorInfo has been affected.
My control template for validation looks like this:
<ControlTemplate x:Key="temp__">
<Border BorderBrush="Orange" BorderThickness="2" CornerRadius="4" SnapsToDevicePixels="True">
<DockPanel>
<Image H开发者_StackOverflow社区orizontalAlignment="Left" VerticalAlignment="Center"
Width="16" Height="16" Margin="-20,0,0,0"
Source="{StaticResource ErrorIcon}"
ToolTip="{Binding ElementName=adornedElement,
Path=AdornedElement.(Validation.Errors),
Converter={helper:ValidationErrorsToStringConverter}}"/>
<AdornedElementPlaceholder Name="adornedElement"/>
</DockPanel>
</Border>
</ControlTemplate>
and is used in a textbox style like this:
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource error_holder}">
In my ViewModel, GetValidationError(string propertyName) uses a switch to validate the appropriate property based on my rules.
The issue is that once a validation has been fired it does not update. For instance, a field can be set as required or not. When loaded, the fields that are required are marked as invalid with an appropriate message. Previously, when a value was entered that was still invalid the error message in the tooltip would update. However this no longer works and the error message still remains as a null value message.
Does anyone know of any changes in the implementation of IDataErrorInfo in 4.0 that may account for this? Any idea how to fix it?
i cant see your binding but you should add ValidatesOnDataErrors.
Ok, so I've sort of fixed this. Rather than use the default implementation that uses Validation.Errors[0] which gives binding errors when the error is fixed, I'd used a converter. Not sure why this resulted in the behaviour originally described, but it did. So I originally changed back to using Validation.Errors[0] and the error message updated correctly. However, this would result in the binding errors returning. Instead I implemented the advice here prevent binding errors and the issue is resolved.
My ControlTemplate now looks like this:
<ControlTemplate x:Key="temp__">
<DockPanel LastChildFill="True">
<Border BorderBrush="Orange" BorderThickness="2" CornerRadius="4" SnapsToDevicePixels="True">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" Color="Orange" BlurRadius="10" />
</Border.Effect>
<DockPanel>
<Image HorizontalAlignment="Left" VerticalAlignment="Center"
Width="16" Height="16" Margin="-20,0,0,0"
Source="{DynamicResource ErrorIcon}"
ToolTip="{Binding ElementName=adornedElement,
Path=AdornedElement.(Validation.Errors)/ErrorContent}"/>
<AdornedElementPlaceholder Name="adornedElement"/>
</DockPanel>
</Border>
</DockPanel>
However, where previously a required field was flagged as an error on loading, this no longer happens. The validation only appears once another error has occurred due to user input. How can I fix this?
精彩评论