WPF 绑定错误 Datagrid 转换器 SelectedItems

发布于 2024-11-16 08:40:38 字数 2376 浏览 1 评论 0原文

我似乎在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘸点软妹酱 2024-11-23 08:40:38

ItemsSource="{绑定路径=ListOfObjects}"

的类型为 Entities.MyType,因此您必须在虚拟机中绑定到此类型的属性。

顺便说一句,您只能绑定到 SelectedItem 而不是 SelectedItems!

您可以做的就是使用 CommandParameters 传递 SelectedItems。

<Button Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=MyDataGrid, Path=SelectedItems}" />

SelectedItems 是 IList 类型!

ItemsSource="{Binding Path=ListOfObjects}"

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.

<Button Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=MyDataGrid, Path=SelectedItems}" />

SelectedItems is type of IList!

自找没趣 2024-11-23 08:40:38

ListOfObjects 中的对象应该具有一些 IsSelected 属性; AFAIK 您无法绑定到 SelectedItems。

然后,您可以将 IsSelected 属性绑定到 DataGridRow 的 IsSelected 属性:

<DataGrid.Resources>
    <Style TargetType="DataGridRow">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />

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:

<DataGrid.Resources>
    <Style TargetType="DataGridRow">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
寻找一个思念的角度 2024-11-23 08:40:38

我只想在 @blindmeis 答案中添加一段有用的代码。

当您使用 @blindmeis 示例中的绑定时,您将获得 SelectedItems 作为对象。我花了一些时间才找到如何将其转换为 IList。给你:

private void DeleteCommand(object param) {
System.Collections.IList itemsList = (System.Collections.IList)SelectedItems;
var collection = items.Cast<item>();

}

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:

private void DeleteCommand(object param) {
System.Collections.IList itemsList = (System.Collections.IList)SelectedItems;
var collection = items.Cast<item>();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文