IsSelected Binding in WPF DataGrid

11,770

that's what helped me finally:

I put in the DataGrid:

VirtualizingStackPanel.VirtualizationMode="Standard"
Share:
11,770
Hodaya Shalom
Author by

Hodaya Shalom

Updated on June 03, 2022

Comments

  • Hodaya Shalom
    Hodaya Shalom almost 2 years

    I have in my Model (Class X) Boolean property: IsSelected, is linked to a WPF DataGrid as follows:

    <DataGrid  SelectedIndex="{Binding SelectedXIndex,Mode=TwoWay}" 
               DataContext="{Binding MyViewModel}" 
               ItemsSource="{Binding ListX}" AutoGenerateColumns="False">
         <DataGrid.RowStyle>
             <Style TargetType="{x:Type DataGridRow}">
                 <Setter Property="IsSelected" 
                         Value="{Binding IsSelected, Mode=TwoWay, 
                                 UpdateSourceTrigger=PropertyChanged}"/>
             </Style>
         </DataGrid.RowStyle>
    </DataGrid>
    

    ListX- ObservableCollection

    IsSelecte- Call to NotifyPropertyChange

    It works great.

    But when I have a lot of rows, that I need to scroll to see them, and I press the button "Select All" that runs the following function, he chooses me only some of the rows and not all: (Even though all the IsSelected on the list is true)

    public void SelectAll()
    {
        ListX.All(c => c.IsSelected = true);
    }
    

    I can not understand why this is happening?

  • dlf
    dlf over 8 years
    If you need to use recycling mode (I do), another option is an attached behavior/codebehind that monitors the grid's OnSelectedCellsChanged event and reacts by updating the (de)selected VMs as indicated (will need to do the reverse too if two-way binding is desired).