How to get the count of selected rows of datagrid following MVVM?

don bradman 626 Reputation points
2022-11-15T05:30:02.153+00:00

I'm trying to get the count of the selected rows of a WPF datagrid and display it in a textblock.

How can I do that following MVVM pattern?

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2022-11-15T09:18:19.427+00:00

    You could refer to the code below.
    Xaml:

      <StackPanel>  
            <DataGrid AutoGenerateColumns="False"  Height="200" x:Name="dg" Width="350" ItemsSource="{Binding Users}" SelectionMode="Extended"   
                      SelectedItem="{Binding SelecedItme ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">  
                    <DataGrid.Columns>  
                    <DataGridTextColumn Header="ID"  Binding="{Binding Id,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>  
                    <DataGridTextColumn Header="Name" Binding="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>  
                </DataGrid.Columns>  
                </DataGrid>  
            <TextBlock x:Name="tb"  Text="{Binding ElementName=dg, Path=SelectedItems.Count, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Width="350" Background="AliceBlue"/>  
        </StackPanel>  
    

    Codebedhind:

    260501-codebehind.txt

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-11-15T10:37:52.98+00:00

    Hi,
    in MVVM you can use an attached property to get SelectedItems directly from DataGrid.

    Demo:

    <Window x:Class="WpfApp1.Window028"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp028"  
            mc:Ignorable="d"  
            Title="donbradman-4481_221115" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <Grid.RowDefinitions>  
          <RowDefinition/>  
          <RowDefinition Height="Auto"/>  
        </Grid.RowDefinitions>  
        <DataGrid ItemsSource="{Binding View}" local:ViewModel.AttProp="True"/>  
        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5">  
          <Label Content="Selected Rows Count: "/>  
          <TextBlock Text="{Binding RowCount}" Margin="5"/>  
        </StackPanel>  
      </Grid>  
    </Window>  
    

    and ViewModel.

    Result:

    260554-x.gif


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.