【发布时间】:2011-09-18 11:49:24
【问题描述】:
在将 datgrid 上的 SelectedItems 集合绑定到我的 ViewModel 中的通用列表时,我似乎遇到了绑定错误。
<DataGrid ItemsSource="{Binding Path=ListOfObjects}" SelectionMode="Extended" SelectionUnit="FullRow" SelectedItems="{Binding Path=ListOfSelectedObjects}" IsEnabled="{Binding Path=IsDoingNothing}">
这是管道位...当我尝试从 DataGrid 中选择一个项目时,我得到的错误是在运行时引发的。它似乎与将“SelectedItem”对象转换为我定义的类型的默认值转换器有关。
我做了一些阅读,我认为我需要某种值转换器?但我对此有点陌生,如果有人可以参考一些可以帮助我的与数据网格的这个问题/管道/应用相关的示例,我会很高兴。
System.Windows.Data Error: 23 : Cannot convert 'Stored Data Backup' from type 'MyType' to type 'System.Collections.Generic.List`1[Entities.MyType]' for 'en-GB' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: CollectionConverter cannot convert from Entities.MyType.
at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'Stored Data Backup' (type 'MyType'). BindingExpression:Path=SelectedExcludedMyType; DataItem='MyTypeManagerViewModel' (HashCode=20097682); target element is 'DataGrid' (Name=''); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: CollectionConverter cannot convert from Dytecna.V001.Entities.MyType.
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)'
编辑:(将标题编辑为)我为我想要的功能绑定到数据网格的错误属性。我已经编辑了上面的 XAML...我想绑定到 SelectedItems,它是复数,而不是 SelectedItem,所以我可以选择多行并将它们绑定到我的 ViewModel 中的列表...
我没有收到上述绑定错误,我只是收到一个:
Error 1 'SelectedItems' property is read-only and cannot be set from markup.
那么我该如何绑定呢?
【问题讨论】:
标签: wpf list datagrid binding converter