Silverlight:如何通过数据上下文获取DataGrid中的数据网格行

发布于 2024-12-04 19:33:45 字数 51 浏览 0 评论 0原文

给定一个 Silverlight 数据网格,如何找到具有特定数据上下文的相应数据网格行?

Given a Silverlight data grid, how can you find the corresponding data grid row with a specific data context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-12-11 19:33:46

这是我编写的一个方法,利用各种自动化对等体来执行此操作:

public DataGridRow GetDataGridRowByDataContext(DataGrid dataGrid, object dataContext)
{
    if (null != dataContext)
    {
        dataGrid.ScrollIntoView(dataContext, null);

        DataGridAutomationPeer automationPeer = (DataGridAutomationPeer)DataGridAutomationPeer.CreatePeerForElement(dataGrid);

        // Get the DataGridRowsPresenterAutomationPeer so we can find the rows in the data grid...
        DataGridRowsPresenterAutomationPeer dataGridRowsPresenterAutomationPeer = automationPeer.GetChildren().
            Where(a => (a is DataGridRowsPresenterAutomationPeer)).
            Select(a => (a as DataGridRowsPresenterAutomationPeer)).
            FirstOrDefault();

        if (null != dataGridRowsPresenterAutomationPeer)
        {
            foreach (var item in dataGridRowsPresenterAutomationPeer.GetChildren())
            {
                // loop to find the DataGridCellAutomationPeer from which we can interrogate the owner -- which is a DataGridRow
                foreach (var subitem in (item as DataGridItemAutomationPeer).GetChildren())
                {
                    if ((subitem is DataGridCellAutomationPeer))
                    {
                        // At last -- the only public method for finding a row....
                        DataGridRow row = DataGridRow.GetRowContainingElement(((subitem as DataGridCellAutomationPeer).Owner as FrameworkElement));

                        // check this row to see if it is bound to the requested dataContext.
                        if ((row.DataContext) == dataContext)
                        {
                            return row;
                        }

                        break; // Only need to check one cell in each row
                    }
                }
            }
        }
    }

    return null;
}

Here's a method I have written to do this leveraging the various automationpeers:

public DataGridRow GetDataGridRowByDataContext(DataGrid dataGrid, object dataContext)
{
    if (null != dataContext)
    {
        dataGrid.ScrollIntoView(dataContext, null);

        DataGridAutomationPeer automationPeer = (DataGridAutomationPeer)DataGridAutomationPeer.CreatePeerForElement(dataGrid);

        // Get the DataGridRowsPresenterAutomationPeer so we can find the rows in the data grid...
        DataGridRowsPresenterAutomationPeer dataGridRowsPresenterAutomationPeer = automationPeer.GetChildren().
            Where(a => (a is DataGridRowsPresenterAutomationPeer)).
            Select(a => (a as DataGridRowsPresenterAutomationPeer)).
            FirstOrDefault();

        if (null != dataGridRowsPresenterAutomationPeer)
        {
            foreach (var item in dataGridRowsPresenterAutomationPeer.GetChildren())
            {
                // loop to find the DataGridCellAutomationPeer from which we can interrogate the owner -- which is a DataGridRow
                foreach (var subitem in (item as DataGridItemAutomationPeer).GetChildren())
                {
                    if ((subitem is DataGridCellAutomationPeer))
                    {
                        // At last -- the only public method for finding a row....
                        DataGridRow row = DataGridRow.GetRowContainingElement(((subitem as DataGridCellAutomationPeer).Owner as FrameworkElement));

                        // check this row to see if it is bound to the requested dataContext.
                        if ((row.DataContext) == dataContext)
                        {
                            return row;
                        }

                        break; // Only need to check one cell in each row
                    }
                }
            }
        }
    }

    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文