简体   繁体   English

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

[英]How get index rows which user is checked in DataGrid in Wpf?

I have DataGrid in Window and I put column inside the DataGrid type "DataGridCheckBox",and I have button in the same Window, but the problem is I don't know how can get index all the rows which user is checked when user click this button. 我在Window中有DataGrid,并将列放在DataGrid类型为“ DataGridCheckBox”内,并且在同一Window中有按钮,但是问题是我不知道如何获得索引,当用户单击此行时将检查该用户所检查的所有行按钮。 the code is : 代码是:

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

behind code: 背后的代码:

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

        }

Your DataGrid has an ItemsSource binding to a collection called Customers . 您的DataGrid有一个ItemsSource绑定到名为Customers的集合。 Furthermore, your DataGridCheckBoxColumn column is bound to a delete property on these objects. 此外,您的DataGridCheckBoxColumn列绑定到这些对象的delete属性。

Within your click handler, simply search for the items in your collection which have this property set to true. 在您的点击处理程序中,只需搜索集合中将此属性设置为true的项目即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM