0

I have DataGrid in Window and I put column inside the DataGrid type "DataGridCheckBox",and I have button in the same Window, but the problem is I don't know how can get index all the rows which user is checked when user click this button. the code is :

<Window x:Class="BenashManage.DeletePerson"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
<Grid HorizontalAlignment="Right" Margin="0,0,0.4,-0.4" Width="546" >
  <DataGrid Margin="15,104,13.6,48.8" Grid.Row="1" Name="GridEdite" ItemsSource="{Binding Customers}" AutoGenerateColumns="False" FlowDirection="RightToLeft" AlternatingRowBackground="AliceBlue" Grid.RowSpan="2" IsSynchronizedWithCurrentItem="True" CanUserResizeColumns="False" CanUserReorderColumns="False" SelectionMode="Single" SelectionUnit="CellOrRowHeader"   >
     <DataGrid.Columns>
         <DataGridCheckBoxColumn Binding="{Binding Path=delete}" Header="حذف البيانات"/>
     </DataGrid.Columns>
  </DataGrid>
  <Button Content="delete" Style="{DynamicResource BlueButtonStyle}" HorizontalAlignment="Left" Foreground="White"  Margin="211,328.2,0,9.8" Grid.Row="2"  Width="118" TextBlock.FontSize="20" Click="OnClicked"/>
</Grid>

behind code:

      private void OnClicked(object sender, RoutedEventArgs e)
        {
    DataGrid GridEdite = new DataGrid();

            foreach (DataGridViewRow r in GridEdite.*****Rows*****)
//in keyword Rows error "'System.Windows.Controls.DataGrid' does not contain a definition for 'Rows' " 
            {
                if (r.Cells["delete"].Checked)
                {
                    r.BackgroundColor = Color.Red; // or do something else
                }
            }

        }
4

1 回答 1

1

DataGrid有一个ItemsSource绑定到一个名为 的集合Customers。此外,您的DataGridCheckBoxColumn列绑定到delete这些对象的属性。

在您的点击处理程序中,只需搜索您的集合中将此属性设置为 true 的项目。

private void OnClicked(object sender, RoutedEventArgs e)
{
   var items = Customers.Where(c => c.delete);
}
于 2013-09-25T19:42:37.307 回答