0

I'm trying to get all row selected in a datagrid. All row are an item of an ObservableCollection<T>, and I would like to know which elements are, or not, selected (could be one, lots or none). Also, I don't want to use a checkbox, would like (if possible) somethink like ctrl + leftMouseClick so select multiple rows.

Here is the xaml code :

                <DataGrid ItemsSource="{Binding ListBinded}" AutoGenerateColumns="False" Name="ListName"
                Style="{StaticResource AzureDataGrid}" Grid.Row="1" FrozenColumnCount="2"
                ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <DataGrid.Resources>
                        <Style x:Key="AlignBottomColumnHeader" BasedOn="{StaticResource AzureDataGridColumnHeader}" TargetType="DataGridColumnHeader">
                            <Setter Property="VerticalContentAlignment" Value="Bottom" />
                        </Style>
                    </DataGrid.Resources>
                    <DataGrid.Columns>
                        <!--Description-->
                        <DataGridTemplateColumn Header="Description" HeaderStyle="{StaticResource AlignBottomColumnHeader}" Width="*" MinWidth="200">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Border BorderThickness="2">
                                        <TextBlock Text="{Binding InfoColumn}"/>
                                    </Border>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

Would love to put the C# code below, but there is no code for now.

EDIT: For me it's not a duplicate, because I don't have a DataGrid in my code below but a ObservableCollection, which does not have a SelectedItem parameter (i'm trying to get the informations in the ViewModelBase class, after an ICommand triggered)

Scondi
  • 27
  • 8
  • Possible duplicate of [Get the multiple selected Row in data-grid in WPF?](https://stackoverflow.com/questions/22143202/get-the-multiple-selected-row-in-data-grid-in-wpf) – BWA Feb 08 '18 at 14:54

1 Answers1

0

If you're using the MVVM pattern you can bind a SelectedRecord property of your VM with SelectedItem of the DataGrid. By this method you always have SelectedValue in your VM.

If you are not using MVVM you can get SelectedIndex property of DataGrid and then cast it to the type you have. Then you can loop over it to get all selected items.

Lion King
  • 495
  • 5
  • 14
  • The problem I get is that I don't have a datagrid in the below code but an ObservableCollection – Scondi Feb 08 '18 at 15:01
  • What do you mean the code you showed, has a datagrid (otherwise how do you expect to show your data), you can just give it a name by x:Name = "MyDataGrid" and do as I told you there. – Lion King Feb 10 '18 at 20:51
  • I already did that, and I can't access to MyDataGrid in my ViewModelBase class – Scondi Feb 12 '18 at 08:30