如何在Wpf中获取在DataGrid中检查用户的索引行?

时间:2013-09-25 19:37:54

标签: c# wpf

我在Window中有DataGrid,我把列放在DataGrid类型“DataGridCheckBox”中,我在同一个Window中有按钮,但问题是我不知道如何获取索引用户被检查时的所有行用户单击此按钮。 代码是:

<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>
代码背后的

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

        }

1 个答案:

答案 0 :(得分:1)

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

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

private void OnClicked(object sender, RoutedEventArgs e)
{
   var items = Customers.Where(c => c.delete);
}
相关问题