如何在Wpf中获取在DataGrid中检查用户的索引行? [英] How get index rows which user is checked in DataGrid in Wpf?

查看:103
本文介绍了如何在Wpf中获取在DataGrid中检查用户的索引行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Window中有DataGrid,我把列放在DataGrid类型DataGridCheckBox中,我在同一个Window中有按钮,但问题是我不知道如何获取索引用户被检查时的所有行用户单击此按钮。代码是:

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. 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" AlternatingRowremoved="AliceBlue" Grid.RowSpan="2" IsSynchronizedWithCurrentItem="True" CanUserResizeColumns="False" CanUserReorderColumns="False" SelectionMode="Single" SelectionUnit="CellOrRowHeader"   >
     <DataGrid.Columns>
         <DataGridCheckBoxColumn Binding="{Binding Path=delete}" Header="deletedata"/>
     </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)
    {
//how can get all index rows which user checked. 
    }

推荐答案

您必须使用datagridview迭代来搜索复选框。您应该在按钮单击事件处理程序中启动它。首先,我们必须得到datagrid的所有行:



You have to use datagridview iteration to search for checked boxes. You should initiate this in your button click event handler. First we have to get all the rows of the datagrid :

public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
    var itemsSource = grid.ItemsSource as IEnumerable;
    if (null == itemsSource) yield return null;
    foreach (var item in itemsSource)
    {
        var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
        if (null != row) yield return row;
    }
}



然后遍历你的网格:


and then iterate through your grid:

var rows= GetDataGridRows(nameofyourdatagrid); 

foreach (DataGridRow r in rows)  
  {  
    DataRowView rv = (DataRowView)r.Item;
    if (r.Checked)
    {
       foreach (DataGridColumn column in nameofyordatagrid.Columns)
       {
           if (column.GetCellContent(r) is TextBlock)
           {
              TextBlock cellContent = column.GetCellContent(r) as TextBlock;
              MessageBox.Show(cellContent.Text);
           }
       }
  } 



当然,您还可以为CellValueChanged事件添加类似的方法,以自动化该过程。希望这有助于:)


Of course, you could also add a similar method to an CellValueChanged Event , to automate the process. Hope this helps :)


这篇关于如何在Wpf中获取在DataGrid中检查用户的索引行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆