使用SelectedValue或SelectedItem的IDataErrorInfo和ComboBox的问题 [英] Issues with IDataErrorInfo and ComboBox using SelectedValue or SelectedItem

查看:54
本文介绍了使用SelectedValue或SelectedItem的IDataErrorInfo和ComboBox的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个DataGrid,其中的DataContext映射到tblCustomer的Customer ObservableCollection的ViewModel.

Hi,

I have a DataGrid with a DataContext mapping to a ViewModel of of Customer ObservableCollection of tblCustomer.

public ObservableCollection<tblCustomer> Customer { get; set; }

在此DataGrid中,我将一个ComboBox嵌入到DataGridTemplateColumn中,如下所示:

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 =真,ValidatesOnDataErrors =真,NotifyOnSourceUpdated =真} </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn>

SelectedValuePath="BankAccount_ID" SelectedValue ="{Binding Path=Customer.FK_BankAccount_ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnSourceUpdated=True}"/> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn>

其中BankAccountList是tblBankAccount的ObservableCollection:

where BankAccountList is an ObservableCollection of tblBankAccount:

public ObservableCollection<tblBankAccount> BankAccountList { get; set; }

DataGrid可以正常工作,并且在用户选择后,ComboBox可以正确插入并显示银行帐户.

但是,如果为客户选择了无效的银行帐户,则不会调用IDataErrorInfo.  客户不能有重复的银行帐户.

据我了解,当使用SelectedItem而不是SelectedValue时,会触发IDataErrorInfo.但是当我使用SelectedItem时,在输出"窗口中出现以下错误:

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'


阿巴斯

推荐答案

由于将ComboBox的SelectedValue属性绑定到嵌套属性,因此将执行tblCustomer.Customer属性的类型,并不仅是视图模型,还是实现了IDataErrorInfo?然后将字符串this [string columnName]索引器 当您在ComboBox中更改选择时应调用.您一定可以绑定ComboBox的SelectedValue属性.

Since you are binding the SelectedValue property of the ComboBox to a nested property, does the type of the tblCustomer.Customer property, and not only the view model, implements the IDataErrorInfo ? Then the string this[string columnName] indexer should get called when you change the selection in the ComboBox. You can surely bind the SelectedValue property of the ComboBox.

如果将ComboBox的SelectedItem属性绑定到源属性,则此源属性必须是tblBankAccount类型,而不是int类型.

If you bind the SelectedItem property of the ComboBox to a source property, this source property must be of type tblBankAccount and not int.

请发布或上传所有相关的源代码,包括视图模型类,以便任何人能够进一步帮助您.

Please post, or upload, all relevant source code including the view model class(es) for anyone to be able to help you any further.


这篇关于使用SelectedValue或SelectedItem的IDataErrorInfo和ComboBox的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆