Alan392
Alan392

Reputation: 695

WPF MVVM DATAGRID select or deselect all rows

this is my DataGrid, how do I know if the user has selected or deselected all rows of the DataGrid ?

Thanks

        <DataGrid ItemsSource="{Binding Dati_Viag}" SelectedItem="{Binding SelectDat}" Style="{DynamicResource ST_DataGrid}" 
                  CellStyle="{DynamicResource St_DataGridCellStyle}" SelectionMode="Extended" Name="Dg_Dati" >
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="col_A" Binding="{Binding Path=A}" Header="A" Width="250" />
            <DataGridTextColumn x:Name="col_U" Binding="{Binding Path=B}" Header="B" Width="250" />
            <DataGridTextColumn x:Name="col_K" Binding="{Binding Path=C}" Header="C"  Width="250" />
        </DataGrid.Columns>
    </DataGrid>

Upvotes: 0

Views: 1757

Answers (1)

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26338

Expose the property IsSelected in your viewmodel, and bind it in DataGrid:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <Trigger Property="IsSelected"
                        Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <Setter Property="BorderBrush"
                        Value="Blue" />
                <Setter Property="BorderThickness"
                        Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

Afer this is done, you can use interactive triggers that will call your viewmodels Command once an item is selected/deselected:

<ie:Interaction.Triggers> <ie:EventTrigger EventName="SelectionChanged"> <ie:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/> </ie:EventTrigger> </ie:Interaction.Triggers>

In where you can do simple calculation, such as:

var allRowsSelected = MyItems.All(x => x.IsSelected)

Upvotes: 1

Related Questions