2

I want to know who are the selected items in the DataGrid (insert them to the collection in the ViewModel).

When I bind to the selected item is changed only when I click on the row (1 row), but when I press ctrl + clicking it remains the first item, why is this happening and is it possible to link all selected items?

Here my DataGrid:

<DataGrid SelectedIndex="{Binding SelectedXIndex}" DataContext="{Binding XViewModel}" SelectedItem="{Binding CurrentX}" ItemsSource="{Binding ListX, Mode=TwoWay}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
     ...
    </DataGrid.Columns>
</DataGrid>

In the XViewModel I have:

SelectedXIndex (int) for the selected index

CurrentX (object of class x) for the current selection

ListX - list of class x

Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111

3 Answers3

1

Have you looked at the "SelectedItems"(not SelectedItem) collection?

TrueEddie
  • 2,183
  • 2
  • 26
  • 36
  • You can get the index like this: MyDataGrid.Items.IndexOf(e.AddedItems[0]) – TrueEddie Feb 12 '13 at 12:21
  • 3
    Error 339 'SelectedItems' property is read-only and cannot be set from markup. – Hodaya Shalom Feb 12 '13 at 12:21
  • Change your binding mode to OneWayToSource – TrueEddie Feb 12 '13 at 12:22
  • I change to this: SelectedItems="{Binding SelectedXs, Mode=OneWayToSource}" and I still recive the error – Hodaya Shalom Feb 12 '13 at 12:24
  • I'm sorry I did not realize you can't bind to SelectedItems. Check out this [http://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm](http://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm) for a workaround. – TrueEddie Feb 12 '13 at 12:27
0

I have posted a technique for allowing a read-only binding to the SelectedItems property of a WPF DataGrid just by extending the DataGrid. You can see my answer at https://stackoverflow.com/a/16953833/62278

Community
  • 1
  • 1
Brian Hinchey
  • 3,601
  • 1
  • 39
  • 36
-1

The first suggestions i made did not work. I leave them just for historical reasons.

I finally think that the problem is that what you want to do is kind of strange - or error prone. You put your bind to SelectedItem (one item) while you want to be able to select multiple.

I would personally put no binding at all in xaml (except the ItemsSource of course) and track selection changes with the selectionChanged event in the back end code. A link with a similar problem supporting this method is found here

Hope you find a better solution

OLD SUGGESTIONS:

this should solve the issue as you seem to be treating it like a single mode selection Datagrid but you do nto explicitely define it as such:

<DataGrid SelectionMode="Single"

If you want the selection mode to be multiple then you should NOT bind to SelectedIndex

I also wonder why you have both SelectedIndex binding and SelectedItem binding. In any case just one should be needed.

Keep just the SelectedItem binding set SelectionMode to Extended and try again

Community
  • 1
  • 1
iltzortz
  • 2,342
  • 2
  • 19
  • 35