2013-09-25 55 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

相關問題