如何从数据网格中的单元格? [英] How to get a cell from DataGrid?

查看:151
本文介绍了如何从数据网格中的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid(.NET Framework 3.5的,WPFToolKit)。我想换边框部分细胞(左或右)。一个,两个或三个。 所以,我怎么能得到一个获得单细胞?而这可能吗? 我找到了一些解决方案,但它们对于.NET 4。

I have a DataGrid (.net framework 3.5, WPFToolKit). And i want to change borders (left or right) of some cells. One, two or three. So how can I get an access to a single cell? And is it possible? I've found some solutions, but they are for .net 4.

推荐答案

您可以扩展的DataGrid并添加以下,注意:这只是一个样本,你不需要做一些,我做了处理:

You can extend the DataGrid and add the following, NOTE: it's just a sample, you don't need to do some of the processing that I am doing:

public DataGridCell GetCell(int row, int column)
    {
        var rowContainer = GetRow(row);

        if (rowContainer != null)
        {
            var presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
            if (presenter == null)
                return null;

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

    public DataGridRow GetRow(int index)
    {
        var row = (DataGridRow)ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // may be virtualized, bring into view and try again
            ScrollIntoView(Items[index]);
            row = (DataGridRow)ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

有关 FindVisualChild 的定义,看的这个网站

这篇关于如何从数据网格中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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