我有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) –