c# - System.Windows.Data 错误 : 1 System. Windows.Data 错误:5

标签 c# wpf data-binding entity-framework-4 selecteditem

全部,

我有一个 WPF 应用程序,它有一个绑定(bind)到 ItemsSource=MyContext.Entity1.Local(这是一个 ObservableCollection)的 DataGrid 控件。 此应用程序内部是一个 DataGridComboBoxColumn,如下所示:

<DataGridComboBoxColumn Header="User"
                        SelectedValueBinding="{Binding Path=USR_ID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                        SelectedItemBinding="{Binding Path=TBL_USR, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                        ItemsSource="{Binding Path=MyEntity.TBL_USR, UpdateSourceTrigger=PropertyChanged, Source={StaticResource VM}}"
                        SelectedValuePath="USR_ID"
                        DisplayMemberPath="USR_WINLOGON" />

我的问题是出现以下错误:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types MyContextType.TBL_USR' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=TBL_USR; DataItem='Entity1Cl_4FDC0A147BE18671D96960BD8695EC4E3656B773570659474EE4FF228884E823' (HashCode=7143675); target element is 'DataGridCell' (Name=''); target property is 'CellContent' (type 'String')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='System.Data.Entity.DynamicProxies.TBL_USR30704B5EF5DAA11FBF2C9B305F529659FBCF422ED6696643EDB375A5D77F5A96' BindingExpression:Path=TBL_USR; DataItem='Entity1Cl_4FDC0A147BE18671D96960BD8695EC4E3656B773570659474EE4FF228884E823' (HashCode=7143675); target element is 'DataGridCell' (Name=''); target property is 'CellContent' (type 'String')

我认为它与 DynamicProxies 有关,但我无法弄清楚是什么原因造成的(.Local?),以及如何围绕此进行编码(至少,不是不使用 TypeConverter 和反射,糟糕)。

要么是这样,要么是与 WPF 行为不当有关(但我在上面的标记中找不到我试图将我的实体设置为字符串的任何地方)。

所以我问,有没有人知道我如何强制 Entity1.Local[n].TBL_USR 只是一个 TBL_USR,或者是否有另一种方式(无需反射)我可以设置我的 SelectedItemBinding?

谢谢。

我正在使用 EF 4.4(数据库优先)和 .Net 4.0。

最佳答案

原来是由于 Mode=TwoWay 和幕后某些不可见的绑定(bind)(对于剪贴板之类的东西)。我曾尝试使用字符串转换器,但是当列失去焦点时,它会产生奇怪的效果,即删除值的显示。这个问题的答案在这里:

Why is my DataGridComboBoxColumn clearing its value when I navigate away from it?

原来这也是这个问题的答案。

关于c# - System.Windows.Data 错误 : 1 System. Windows.Data 错误:5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13500363/

相关文章:

c# - 如何在不使用 C# 将其设置为默认打印机的情况下直接打印到 zebra 打印机?

c# - 对于在 ASP.NET Web 角色中执行的套接字读取循环来说,缓冲区大小越大越好吗?

c# - 随时间变化的数据聚合

c# - 模拟 Windows 8 开始菜单磁贴布局引擎

asp.net - 为本地化设置 TemplateField HeaderText 动态

c# - 值(value)随时间呈指数衰减

wpf - Graphic 元素使用位图好还是 Xaml 好

c# - 不允许发送或接收数据的请求,因为未连接套接字(仅当我第二次连接时)

c# - WPF ComboboxItem 绑定(bind)

wpf - 此 View 不允许使用“EditItem” - 数据绑定(bind)问题