How to get ObservableCollection to work with IDataError
I have dialog box that displays individual elements of an ObservableCollection(a collection o ints). As the user changes individual values of the collection, I want to validate the change.
In my VM, there is only a property for the collection, and my XAML is bound to the individual elements.
So how do I get the validation property indexer to be called when an item in the collection changes?
I'm still pretty new to WPF so I image there's a simple solution to this..
Thanks for you help.
Here's the XAML. There's an equivalent text box for each of the 10 channels
<TextBox Grid.Row="0" Grid.Column="1" Name="chan01"
HorizontalAlignment="Left" Width="60"
开发者_如何学编程 Text="{Binding ChannelList[0].ChannelNumber, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True}" />
Here's some of the code behind in my VM. The grp_ object is the Model
/// <summary>
/// Scan channel list.
/// </summary>
public ChannelNumberCollection ChannelList
{
get
{
return grp_.ChannelList;
}
set
{
grp_.ChannelList = value; NotifyPropertyChanged("ChannelList");
}
}
public string this[string propertyName]
{
get
{
switch (propertyName)
{...
I don't think your approach will work because in order for the ValidatesOnDataError property to work, the target of the Binding must implement IDataErrorInfo. In your scenario, the ChannelNumber property is the target, not the ChannelNumberCollection itself and your code snippet indicates that the collection implements IDataErrorInfo.
I think you either need to:
- Make sure that each item in the ChannelNumberCollection implements IDataErrorInfo (my recommendation)
or
- Create a ValidationRule which can be added to the Binding.ValidationRules property of the ChannelNumber binding
精彩评论