我有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
}
}
}
爲行創建一個合適的視圖模型並在ViewModel級別處理它。 [UI不是數據](http://stackoverflow.com/a/14382137/643085) –