WPF MVVM DATAGRID选择或取消选择所有行

时间:2015-01-02 13:55:51

标签: wpf select mvvm datagrid rows

这是我的DataGrid,我如何知道用户是否已选择或取消选择DataGrid的所有行?

由于

        <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>

1 个答案:

答案 0 :(得分:0)

在您的视图模型中公开属性IsSelected,并将其绑定到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>

完成此操作后,您可以使用交互式触发器,在选择/取消选择项目后调用您的视图模型Command

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

您可以在哪里进行简单的计算,例如:

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