This XAML element is bound to a ListCollectionView in my View Model:
<Style x:Key="ErrorStyle" TargetType="{x:Type Control}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="Salmon"/>
</Trigger>
</Style.Triggers>
</Style>
...
<controls:AutoCompleteBox Grid.Column="1" Grid.Row="0" Margin="5" Height="20" Width="270" HorizontalAlignment="Left" VerticalAlignment="Center"
Name="typeName"
Style="{StaticResource ErrorStyle}"
Text="{Binding Path=AirframeCollectionView/TypeName, UpdateSourceTrigger=LostFocus, Mode=TwoWay,
ValidatesOnNotifyDataErrors=True,
NotifyOnValidationError=True,
ValidatesOnExceptions=True}"
ItemsSource="{Binding Path=TypeNames}"
IsTextCompletionEnabled="True"
FilterMode="Contains"
MinimumPrefixLength="3">
</controls:AutoCompleteBox>
The ListCollectionView is defined thus:
public ListCollectionView AirframeCollectionView
{
get
{
return this.airframeCollectionView;
}
set
{
this.airframeCollectionView = value;
this.RaisePropertyChanged("AirframeCollectionView");
}
}
and initialised:
this.currentAirframes = new ObservableCollection<Airframe>(this.UnitOfWork.Airframes.GetAirframesForRegistration(this.SearchRegistration));
this.AirframeCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(this.currentAirframes);
When validating AirframeCollectionView/TypeName I'm using the INotifyDataErrorInfo interface hence:
private readonly Dictionary<string, ICollection<string>> validationErrors = new Dictionary<string, ICollection<string>>();
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors
{
get { return this.validationErrors.Count > 0; }
}
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) || !this.validationErrors.ContainsKey(propertyName))
{
return null;
}
return this.validationErrors[propertyName];
}
private void RaiseErrorsChanged(string propertyName)
{
if (this.ErrorsChanged != null)
{
this.ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
}
To raise an error I've been doing this:
this.validationErrors["AirframeCollectionView/TypeName"] = validationErrors;
this.RaiseErrorsChanged("AirframeCollectionView/TypeName");
This doesn't trigger the error response in the UI however. I changed the property name from "AirframeCollectionView/TypeName" to "TypeName" but that doesn't work either. In the debugger I've confirmed that validationErrors gets loaded with errors and that ErrorsChanged is fired with the supplied property name.
Note that this was all working when I implemented INotifyDataErrorInfo in the Model rather than the ViewModel but for various reasons I want the implementation to be in ViewModel.
Question
What property name format do I have to use when setting up the DataErrorsChangedEventArgs and triggering ErrorsChanged? Or is there some other structural problem I have here?
I conclude that you can't get INotifyDataErrorInfo to talk to the UI when using a ListCollectionView property in your view model and firing ErrorsChanged from the view model. To get this working therefore I have:
This keeps bespoke validation out of the model and all is well.