c# - 如何在代码中访问 DataGridCell 的数据对象?

标签 c# wpf c#-4.0 data-binding datagrid

基本上,我绑定(bind)了数据网格,使其类似于科目时间表 - 每行代表一个学期的科目,该学期内的每个单元格代表一个科目。

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

我可以使用一些可视树方法来找到用户将新主题拖到的 DataGridCell,但我不知道如何访问单元格绑定(bind)到它的值(主题)以替换新主题的空白/占位符值。有没有一种方法可以访问潜在的值(value),或者我应该重构我创建这个程序的整个方法?

An example of the grid and the subjects to be dragged onto it

最佳答案

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

// 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];

如果您的行数据更复杂,您需要编写一个相应的方法来翻译 Column属性和行数据项设置为行数据项上的特定值。


编辑:

如果您将数据放入的单元格不是选定的单元格,一种选择是获取 DataGridRow DataGridCell属于,使用 VisualTreeHelper :

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

然后您有了该行,可以按上述方式继续操作。


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

行为提供了一种非常直接的方式来从 C# 代码(而不是 XAML)扩展控件的功能,同时保持代码隐藏的清晰和简单(如果您遵循 MVVM,这不仅很好)。行为以可重用的方式设计,并且不受您的特定控制的约束。

Here's a good introduction

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

写一个DropBehavior对于您的 TextBlock 控件(或您想要在 DataGridCells 中处理掉落的任何控件)。基本思想是根据控件的 OnAttached() 方法中单元格的事件注册操作。

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;

    }
}

注意,从行数据和Column中解析单个单元格的数据属性(property)变得过时。

然后使用 ContentTemplate 将此行为附加到放置在单元格中的 TextBlocks的 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>

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

System.Windows.Interactivity.dll

我还没有测试过它,但我希望它对你有用并且你明白了......

关于c# - 如何在代码中访问 DataGridCell 的数据对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15361605/

相关文章:

c# - 登录失败 : the user has not been granted the requested logon type at this computer

c# - 如何在WPF中使用Flyout?

wpf - VirtualizingStackPanel 与虚拟化列表

c# - 用 linq 分组;但有条件

c# - 将 CheckedListBox 绑定(bind)到设置

c# - 需要在 ASP.NET 网络表单中包装表行和控件

c# - 在 catch block 中返回?

c#-4.0 - 使用 >、>=、<、<= 运算符的 Entity Framework 字符串数据类型 : Lambda expression

c# - 如何在 ListBox 中显示类的属性?

wpf - 复合类中的 C++/CLI DLL 和 ObservableCollections