DataGrid如何获取CurrentCell的值

发布于 2024-09-10 04:23:43 字数 1103 浏览 6 评论 0原文

我必须访问 DataGrid 活动单元格(周围有黑色边框的单元格)下方的值。

幸运的是,DataGrid 有很多属性,例如 CurrentCell、CurrentItem、SelectedCells、SelectedItem 和 SelectedItems 似乎为我提供了我想要的数据。

但是我还没有弄清楚如何以简单的方式访问单元格。我还更改了......

SelectionMode="Single" SelectionUnit="Cell"

属性,但最终我不得不做这样的事情:

DataGridCellInfo cellInfo = dataGrid.CurrentCell;

if(null != cellInfo && cellInfo.IsValid)
{
    object[] array = cellInfo.Item as object[];
    if (null != array && cellInfo.Column.DisplayIndex >= 0 && cellInfo.Column.DisplayIndex < array.Length) 
    {
        object cellValue = array[cellInfo.Column.DisplayIndex];
        if (null != cellValue) 
        {
            // Here we are
        }
    }
}

在我的示例中,行是通过包含各种对象类型的对象数组构建的。我知道我可以在 cellInfo.Column 上执行绑定(在强制转换之后),但这不是重点。我的问题是,如果我真的犯了错误,因为我无法想象像 DataGrid 这样强大的软件,如果不做这么多编码就无法为我提供所需的值。

我错过了什么,或者获取当前单元格值真的如此复杂。

更新

正如 Quartermeister 在他的非常好的答案中解释的那样,没有一个属性可以访问细胞价值,这是有一个有意义的原因。此外,如果您让用户重新排列列,请小心使用 DisplayIndex,就像我在示例中所做的那样。

I have to access the value that underlies the active cell of a DataGrid (The cell with the black border around it).

Luckily DataGrid has a lot of properties such as CurrentCell, CurrentItem SelectedCells SelectedItem and SelectedItems seeming to provide me with the data I want.

However I have not figured out how to access the cell in an easy way. I also have changed the …

SelectionMode="Single" SelectionUnit="Cell"

...properties but in the end I had to do something like this:

DataGridCellInfo cellInfo = dataGrid.CurrentCell;

if(null != cellInfo && cellInfo.IsValid)
{
    object[] array = cellInfo.Item as object[];
    if (null != array && cellInfo.Column.DisplayIndex >= 0 && cellInfo.Column.DisplayIndex < array.Length) 
    {
        object cellValue = array[cellInfo.Column.DisplayIndex];
        if (null != cellValue) 
        {
            // Here we are
        }
    }
}

In my example, the row is built through an object-array containing various object-types. I'm aware that I could execute the binding on the cellInfo.Column (after a cast), however that is not the point. My question is, if I make something really wrong, because I cannot imaging that such a powerful piece of software as DataGrid is, can not provide me the desired value without doing such a lot of coding.

What have I missed or is it really such complicated to get the current cells value.

UPDATE

As Quartermeister explained in his very good answer, there is not a single property to access the cells value and this is has a meaningfull cause. besides, beware of using the DisplayIndex as I do in my example if you let the user rearrange the columns.

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

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

发布评论

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

评论(1

水溶 2024-09-17 04:23:43

确实有那么复杂。问题是 DataGridColumn 不一定绑定到单个值。 DataGridTemplateColumn,用于例如,只有一个 DataTemplate。模板可能使用行对象中的多个值,甚至根本不使用任何值,因此无法有效地返回该单元格的单个值。

如果列是 DataGridBoundColumn(例如 DataGridTextColumn)。正如你所说,你可以通过执行Binding来获取值。例如,您可以执行类似的操作(请注意,这对于使用 ElementName 或relativeSource 的绑定不起作用,但您可能不会在 DataGridBoundColumn 中使用它们):

var cellInfo = dataGrid.CurrentCell;
if (cellInfo != null)
{
    var column = cellInfo.Column as DataGridBoundColumn;
    if (column != null)
    {
        var element = new FrameworkElement() { DataContext = cellInfo.Item };
        BindingOperations.SetBinding(element, FrameworkElement.TagProperty, column.Binding);
        var cellValue = element.Tag;
    }
}

请注意,您可能不想使用DisplayIndex 属性,因为如果用户手动拖动列以重新排序,则可以更改该属性。

It really is that complicated. The problem is that a DataGridColumn isn't necessarily bound to a single value. A DataGridTemplateColumn, for example, just has a DataTemplate. The template may use multiple values from the row object or even no values at all, so there is no way to usefully return a single value for that cell.

You can be guaranteed to have a single value if the column is a DataGridBoundColumn (such as a DataGridTextColumn). As you said, you can get the value by executing the Binding. For example, you could do something like this (note that this won't work for a binding that uses ElementName or RelativeSource, but you probably won't use those in a DataGridBoundColumn):

var cellInfo = dataGrid.CurrentCell;
if (cellInfo != null)
{
    var column = cellInfo.Column as DataGridBoundColumn;
    if (column != null)
    {
        var element = new FrameworkElement() { DataContext = cellInfo.Item };
        BindingOperations.SetBinding(element, FrameworkElement.TagProperty, column.Binding);
        var cellValue = element.Tag;
    }
}

Note that you probably don't want to use the DisplayIndex property, since that can be changed if the user manually drags the columns to reorder them.

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