0

I've a VB.net WPF application that display data on a datagrid. I need to get the selected rows;

XAML

<DataGrid Grid.Column="1" Loaded="{s:Action RefreshMyTable}" Grid.ColumnSpan="5" Grid.Row="1"  Grid.RowSpan="17" SelectionChanged="{s:Action test}"  AutoGenerateColumns="True" ColumnWidth="*" SelectedValuePath="ID"  SelectedItem="{Binding Path=SelectedItemDG, Mode=TwoWay}" ItemsSource="{Binding myCollection}">

VM

Public Property SelectedItemDG As New myObject
Public Property SelectedItemDGCollection As New BindableCollection(Of myObject)
Public Sub test(ByVal sender As Object, ByVal e As EventArgs)
        SelectedItemDGCollection.Add(SelectedItemDG)
End Sub



Public Sub btntestmultiselection()
        For Each x In SelectedItemDGCollection
            Debug.WriteLine(x.ID)
        Next
  
    End Sub`

I can get the item selected if only one row is selected, but if many rows are selected in this way I get only the latest. I found some answers here that mention the property SelectedItems instead of SelectedItem, but I can't find it in wpf datagrid. Of course I would avoid code behind since I'm using mvvm. Any advice?

BionicCode
  • 1
  • 4
  • 28
  • 44
  • The WPR DataGrid has a SelectedItems [property](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=windowsdesktop-8.0#properties) (it definitely has!). Code-behind does not violate MVVM or is something bad in general. It's a question of C# vs XAML and not MVVM. – BionicCode Apr 24 '23 at 07:37

0 Answers0