2013-09-25 29 views
0

我有DataGrid在窗口中,我把列内DataGrid类型“DataGridCheckBox”,并且我有同一个窗口中的按钮,但问题是我不知道如何获取索引所有行用户检查时用户点击这个按钮。 代码:如何获取用Wpf中的DataGrid检查用户的索引行?

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

     } 
+0

为行创建一个合适的视图模型并在ViewModel级别处理它。 [UI不是数据](http://stackoverflow.com/a/14382137/643085) –

回答

1

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

在您的点击处理程序中,只需搜索您的收藏中具有此属性设置为true的项目。

private void OnClicked(object sender, RoutedEventArgs e) 
{ 
    var items = Customers.Where(c => c.delete); 
} 
+0

当应用此代码时,我定义了列表名Customer但“delete”不能访问此列Error“Error'object'不包含'delete'的定义并且没有扩展方法'delete'“ – ITInWorld

相关问题