我怎样才能访问DataGridCell的数据对象的代码? [英] How can I access the data object of a DataGridCell in code?

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

问题描述

基本上,我已经绑定的数据网格,以便它类似于主题的时间表 - 每一行代表的科目一学期,这学期中的每个单元代表一个主题



我现在正在尝试添加拖放功能,让您可以将更多的科目到网格,而这将更新基础数据结构。



我可以用一些视觉树的方法找到DataGridCell用户正在拖动新课题,但我不知道如何访问值(标的),该单元被绑定到它,以替换为新主题空白/占位符值。 ?是否有访问底层价值的方式,或者我应该调整自己创建这个项目的整个法




解决方案

为了得到DataGridCell的数据,你可以用它的DataContext和属性。如何做到这一点正是取决于你的行数据是什么,也就是你把哪些项目DataGrid的的ItemsSource 收藏。假设你的项目 [对象] 数组:



<预类=郎-CS prettyprint-覆盖> //假设这是一个对象数组,对象[],这可以让你的
//行数据如您在DataGrid的ItemsSource的集合
变种rowData让他们=(对象[]) DataGrid.SelectedCells [0] .Item;
//这让你单细胞对象
VAR celldata = rowData [DataGrid.SelectedCells [0] .Column.DisplayIndex]

如果您的行数据比较复杂,你需要写一个转换的<$ c。将根据方法$ C>列属性和行数据项上的行数据项的特定值。






编辑:



如果单元格你把你的数据到是不是所选单元格,一种选择是让 DataGridRow 到的 DataGridCell 所属,使用 VisualTreeHelper



<预类=郎-CS prettyprint-覆盖> VAR父= VisualTreeHelper.GetParent(栅格单元);
,而(父= NULL&放大器;!&安培;!parent.GetType()= typeof运算(DataGridRow))
{
父= VisualTreeHelper.GetParent(父);
}
变种的DataRow =父母;



然后你行,并可以执行上述操作。






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



行为提供了一个非常简单的方法来扩展从C#代码控制的能力,而在XAML,但保持你的代码隐藏简单明了的(这不仅是不错的,如果你以下MVVM )。行为是设计方式,他们可以重复使用,而不是绑定到特定的控制。



这里有一个很好的介绍



有关你的特殊情况下,我只能给你做什么的想法:



写1 DropBehavior 为您的TextBlock控件(或任何你想你的DataGridCells内部人控制,它处理的下降。其基本思想是根据行动,在控制的 OnAttached()方法单元格的EVNT注册。



<预类=郎-CS prettyprint-覆盖> 公共类DropBehavior:行为< TextBlock的>
{
保护覆盖无效OnAttached()
{
AssociatedObject.MouseUp + = AssociatedObject_MouseUp;
}

私人无效AssociatedObject_MouseUp(对象发件人,MouseButtonEventArgs E)
{
//处理鼠标高达
$ b $发生了什么b //检查的要求,有数据一直拖着,等
//获取基础数据,现在简称为AssociatedObject的
VAR的DataContext的cellData = AssociatedObject.DataContext;

}
}



需要注意的是,解析数据从行数据和单细胞列属性变得过时。



然后,您可以将这种行为的TextBlocks,你把你的细胞中,使用的的ContentTemplate CellStyle 您的DataGrid的:



<预类=郎咸平的XML prettyprint-覆盖> <数据网格> ;
< DataGrid.CellStyle>
<风格的TargetType =DataGridCell>
< setter属性=的ContentTemplate>
< Setter.Value>
<&DataTemplate的GT;
< TextBlock的文本={结合}>
< I:Interaction.Behaviors>
< yourns:DropBehavior />
< /我:Interaction.Behaviors>
< / TextBlock的>
< / DataTemplate中>
< /Setter.Value>

< /二传手>
< /样式和GT;
< /DataGrid.CellStyle>
< / DataGrid的>

您可以找到行为< T> 基类中的




System.Windows.Interactivity.dll




我没有测试过,但我希望它为你和你的想法......


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.

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?

解决方案

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.


EDIT:

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.


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

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.

Here's a good introduction

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

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.

Then you attach this behavior to TextBlocks, which you put inside your cells, using the ContentTemplate of the CellStyle of your DataGrid:

<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>

You can find the Behavior<T> baseclass in

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天全站免登陆