WPF Datagrid行索引始终为0

时间:2015-04-16 17:57:24

标签: c# wpf wpf-controls wpfdatagrid

我正在尝试获取当前的编辑行索引,但它会保持返回0,我认为是因为网格还没有可视化,所以我创建了一个调度程序来运行代码,但仍然无法正常工作

以下是我的代码

private void grdUser_RowEditingEnd(object sender, DataGridRowEditEndingEventArgs e )
{
        Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
        {                
            int currentRowIndex = this.grdUser.ItemContainerGenerator.IndexFromContainer(this.grdUser.ItemContainerGenerator.ContainerFromItem(this.grdUser.CurrentItem));
            MessageBox.Show(currentRowIndex.ToString());

        }));
}

这是获取编辑的行索引的其他方法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用eventargs并使用它来获取索引。您可以参考以下代码。

private void grdUser_RowEditingEnd(object sender, DataGridRowEditEndingEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
        {
            int currentRowIndex = this.grdUser.ItemContainerGenerator.IndexFromContainer(e.Row);
            MessageBox.Show(currentRowIndex.ToString());

        }));            
    }
相关问题