WPF 绑定错误 Datagrid 转换器 SelectedItems
我似乎在将 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.
那么我该如何绑定它呢?
I seem to be getting a binding error with a binding of the SelectedItems collection on a datgrid to a generic list in my ViewModel.
<DataGrid ItemsSource="{Binding Path=ListOfObjects}" SelectionMode="Extended" SelectionUnit="FullRow" SelectedItems="{Binding Path=ListOfSelectedObjects}" IsEnabled="{Binding Path=IsDoingNothing}">
That's the plumbing bit... the error I get is thrown at runtime when I try to select an item from the DataGrid. It appears something to do with the default value converter converting the 'SelectedItem' object to my defined type.
I have done a little reading and I think I need a value converter of some kind? But I am a bit of a newb to this and would love it if someone could ref some examples that could help me that are related to this problem/plumbing/application of the datagrid.
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)'
EDIT: (edited the title to) I was binding to the wrong property of the datagrid for my desired function. I have edited the XAML above... I want to bind to the SelectedItems, that's plural, not SelectedItem, so I can selectr multiple rows and bind them to a list in my ViewModel...
I don't get the above binding error, I just get a:
Error 1 'SelectedItems' property is read-only and cannot be set from markup.
So how do I bind to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
的类型为 Entities.MyType,因此您必须在虚拟机中绑定到此类型的属性。
顺便说一句,您只能绑定到 SelectedItem 而不是 SelectedItems!
您可以做的就是使用 CommandParameters 传递 SelectedItems。
SelectedItems 是 IList 类型!
is of type Entities.MyType, so you have to bind to a property of this type in your vm.
btw you just can bind to SelectedItem and not SelectedItems!
What you can do is to pass the SelectedItems with CommandParameters.
SelectedItems is type of IList!
ListOfObjects 中的对象应该具有一些 IsSelected 属性; AFAIK 您无法绑定到 SelectedItems。
然后,您可以将 IsSelected 属性绑定到 DataGridRow 的 IsSelected 属性:
Objects in ListOfObjects should instead have some IsSelected property; AFAIK you can't bind to SelectedItems.
You can then bind the IsSelected property to the IsSelected property of DataGridRow:
我只想在 @blindmeis 答案中添加一段有用的代码。
当您使用 @blindmeis 示例中的绑定时,您将获得 SelectedItems 作为对象。我花了一些时间才找到如何将其转换为 IList。给你:
}
I just want to add piece of useful code to @blindmeis answer.
When you use binding from @blindmeis example you will get SelectedItems as object. It take me some time to find how cast it to IList. Here you are:
}