WPF Binding Error Datagrid Converter SelectedItems
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?
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!
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}" />
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>();
}
精彩评论