如何在代码中访问DataGridCell的数据对象? [英] How can I access the data object of a DataGridCell in code?

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

问题描述

基本上我已经绑定了datagrid,使其类似于主题的时间表 - 每一行代表一个学期的学科,而该学期的每个单元格代表一个主题。

Basically I've bound the datagrid so that it resembles a timetable of subjects - each row represents a semester of subjects, and each cell within that semester represents a subject.

我现在正在尝试添加拖放功能,以便您可以将其他主题拖放到网格上,这样可以更新基础数据结构。

I'm now trying to add drag and drop functionality so that you can drag additional subjects onto the grid, and this will update the underlying datastructure.

我可以使用一些可视化的树方法来查找用户正在拖动新主题的DataGridCell,但是我不知道如何访问该值)单元格绑定到它,以便用新主题替换空白/占位符值。有没有办法访问底层价值,还是应该重组我的整个创建此程序的方法?

I can use some visual tree methods to find the DataGridCell that the user is dragging the new subject to, but I don't know how to access the value (the subject) that the cell is bound to it in order to replace the blank/placeholder value with the new subject. Is there a way to access the underlying value or should I restructure my entire method of creating this program?

推荐答案

要获取DataGridCell的数据,可以使用它的DataContext和 Column 属性。如何做到这一点完全取决于您的行数据,即您在DataGrid的 ItemsSource 集合中放置的项目。假设你的项目是 object [] 数组:

To get the data of the DataGridCell, you can use it's DataContext and the Column property. How to do that exactly depends on what your row data is, i.e. what items you put in the ItemsSource collection of the DataGrid. Assuming your items are object[] arrays:

// Assuming this is an array of objects, object[],this gets you the 
// row data as you have them in the DataGrid's ItemsSource collection
var rowData = (object[]) DataGrid.SelectedCells[0].Item;
//  This gets you the single cell object
var celldata = rowData[DataGrid.SelectedCells[0].Column.DisplayIndex];

如果你的行数据更复杂,你需要编写一个相应的方法来翻译属性,行数据项到行数据项的特定值。

If your row data is more complex, you need to write an according method which translates the Column property and the row data item to the specific value on your row data item.

编辑:

如果将数据放入的单元格不是所选单元格,则可以使用一个选项来获取 DataGridRow VisualTreeHelper DataGridCell :c $ c>

If the cell you drop your data into is not the selected cell, one option is to get the DataGridRow to which the DataGridCell belongs, using VisualTreeHelper:

var parent = VisualTreeHelper.GetParent(gridCell);
while(parent != null && parent.GetType() != typeof(DataGridRow))
{
    parent = VisualTreeHelper.GetParent(parent);
}
var dataRow = parent;

然后你有这行,可以像上面那样继续。

Then you have the row and can proceed as above.

此外,关于您的问题,您是否应该重新考虑该方法,我建议使用自定义的 WPF行为

Furthermore, regarding your question whether you should reconsider the method, I would suggest using custom WPF behavior s.

行为提供了一个非常直接的方法来扩展控件的功能从C#代码,而不是XAML,但保持你的代码清晰和简单(这不仅仅是很好,如果你正在追踪MVVM )。行为是以可重复使用的方式设计的,而不受您特定的控制。

Behaviors provide a very straight forward way to extend the control's capabilities from C# code, rather the XAML, yet keeping your codebehind clear and simple (which is not only nice to have if you're following MVVM). Behaviors are designed in way that they are reusable and not bound to your specific control.

这是一个很好的介绍

对于你的特殊情况,我只能给你一个想法:

For your special case, I can only give you an idea of what to do:

为您的TextBlock控件(或您在DataGridCells内部处理的任何控件)编写一个 DropBehavior ,其基本思想是根据您控件的 OnAttached()方法中的单元格注释。

Write one DropBehavior for your the TextBlock control (or whatever control you want inside your DataGridCells, which handles the drop. The basic idea is to register according actions to the evnt of the cells in the OnAttached() method of your control.

public class DropBehavior : Behavior<TextBlock>
{
    protected override void OnAttached()
    {
        AssociatedObject.MouseUp += AssociatedObject_MouseUp;
    }

    private void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e)
    {
        // Handle what happens on mouse up

        // Check requirements, has data been dragged, etc.
        // Get underlying data, now simply as the DataContext of the AssociatedObject
        var cellData = AssociatedObject.DataContext;

    }
}

请注意,解析数据行数据中的单个单元格和属性已过时。

Note that, parsing the data of the single cell from the row data and the Column property becomes obsolete.

然后,使用 ContentTemplate 的<$您的DataGrid的c $ c> CellStyle

<DataGrid>
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{Binding}">
                            <i:Interaction.Behaviors>
                                <yourns:DropBehavior/>
                            </i:Interaction.Behaviors>
                        </TextBlock>
                    </DataTemplate>
                </Setter.Value>

            </Setter>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

您可以找到行为< T>


System.Windows.Interactivity.dll

System.Windows.Interactivity.dll

我没有测试过,但我希望它适用于你,你会得到想法...

I haven't tested it, but I hope it works for you and you get the idea...

这篇关于如何在代码中访问DataGridCell的数据对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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