Datagrid:如何获取SelectedCell的SelectedItem? [英] Datagrid: How to get the CurrentCell of the SelectedItem?

查看:1115
本文介绍了Datagrid:如何获取SelectedCell的SelectedItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF datagrid的代码中,如何从我的dataGrid.SelectedItem(In代码)中获取currentCell?



许多谢谢,

解决方案

请尝试从 post



您可以从 dataGrid.SelectedIndex 和列by dataGrid.CurrentColumn.DisplayIndex

  public static DataGridCell GetCell(DataGrid dataGrid,int row,int column)
{
DataGridRow rowContainer = GetRow(dataGrid,row);
if(rowContainer!= null)
{
DataGridCellsPresenter Presenter = GetVisualChild< DataGridCellsPresenter>(rowContainer);

//尝试获取单元格但可能会被虚拟化
DataGridCell cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if(cell == null)
{
//现在尝试带入视图并检索单元格
dataGrid.ScrollIntoView(rowContainer,dataGrid.Columns [column]);

cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}

返回单元格;
}

返回null;
}

编辑

  public static DataGridRow GetRow(DataGrid dataGrid,int index)
{
DataGridRow row =(DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if(row == null)
{

dataGrid.ScrollIntoView(dataGrid.Items [index]);
dataGrid.UpdateLayout();

row =(DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
}

返回行;
}

您可以找到完整的源代码这里(寻找代码在页面)


Within a WPF datagrid's code behind, how do I get the currentCell from my dataGrid.SelectedItem (In Code)?

Many Thanks,

解决方案

Try this from post

You can retrieve row from dataGrid.SelectedIndex and column by dataGrid.CurrentColumn.DisplayIndex

public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
        {
            DataGridRow rowContainer = GetRow(dataGrid, row);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

                // try to get the cell but it may possibly be virtualized
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);

                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }

                return cell;
            }

            return null;
}

Edit

public static DataGridRow GetRow(DataGrid dataGrid, int index)
    {
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {

            dataGrid.ScrollIntoView(dataGrid.Items[index]);
            dataGrid.UpdateLayout();

            row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        }

        return row;
    }

you can find the complete source code here (look for code at end of page)

这篇关于Datagrid:如何获取SelectedCell的SelectedItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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