Hi,
I have a DataGrid with a DataContext mapping to a ViewModel of of Customer ObservableCollection of tblCustomer.
public ObservableCollection<tblCustomer> Customer { get; set; }
In this DataGrid, I have a ComboBox embeded in a DataGridTemplateColumn, as follows:
<DataGridTemplateColumn Header="Bank Account" x:Name="BankAccountCBox" ><DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock ><TextBlock.Text><Binding Path="Customer.tblBankAccount.BankAccountName" Mode="OneWay" /></TextBlock.Text></TextBlock></DataTemplate ></DataGridTemplateColumn.CellTemplate ><DataGridTemplateColumn.CellEditingTemplate><DataTemplate><ComboBox Style="{StaticResource DataValidationStyle}" ItemsSource="{Binding DataContext.BankAccountList, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}" DisplayMemberPath="BankAccountName"
SelectedValuePath="BankAccount_ID" SelectedValue ="{Binding Path=Customer.FK_BankAccount_ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnSourceUpdated=True}"/></DataTemplate></DataGridTemplateColumn.CellEditingTemplate></DataGridTemplateColumn>
where BankAccountList is an ObservableCollection of tblBankAccount:
public ObservableCollection<tblBankAccount> BankAccountList { get; set; }
The DataGrid works fine and, upon the user's selection, the ComboBox inserts and displays Bank Accounts correctly.
However, IDataErrorInfo is not invoked if an invalid Bank Account is selected for a customer. A customer cannot have duplicate Bank Accounts.
As I understand it, IDataErrorInfo fires on when SelectedItem is used instead of SelectedValue; but when I use SelectedItem, I get the following error in the Output window:
A first chance exception of type 'System.NotSupportedException' occurred in System.dll System.Windows.Data Error: 23 : Cannot convert 'PropertyManager.Domain.tblBankAccount' from type 'tblBankAccount' to type 'System.Int32' for 'en-GB' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from PropertyManager.Domain.tblBankAccount. at System.ComponentModel.TypeConverter.GetConvertFromException(Object value) at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)' A first chance exception of type 'System.NotSupportedException' occurred in PresentationFramework.dll System.Windows.Data Error: 7 : ConvertBack cannot convert value 'PropertyManager.Domain.tbltBankAccount' (type 'tblBankAccount'). BindingExpression:Path=Customer.FK_BankAccount_ID; DataItem='EntityDataModel`1' (HashCode=13726957); target element is 'ComboBox' (Name='BankAccountCBox'); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from PropertyManager.Domain.tblBankAccount. at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward) at MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture) at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)' 'PropertyManager.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemCore\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemCore.dll'Your suggestion is greatly appreciated.
Many thanks.
Abbas