为在代码中创建的DataGridTemplateColumn添加数据绑定 [英] Add data-binding for DataGridTemplateColumn created in code

查看:182
本文介绍了为在代码中创建的DataGridTemplateColumn添加数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

是否可以在其中定义 DataTemplate XAML并将其实例化为代码(而不是通过 FindResource 检索单例)并修改其 VisualTree ,然后再发送到 DataTemplate 是必需的,例如 DataGridTemplateColumn.CellTemplate

Is there a way to define a DataTemplate in XAML and instantiate it in code (rather than retrieve singleton by FindResource) and modify its VisualTree before sending to where a DataTemplate is required such as DataGridTemplateColumn.CellTemplate?

背景:

我正在显示二维数组 data [] [] 通过自己添加 DataGridTemplateColumn 列来创建 DataGrid ,并且有 DataTemplate 知道如何呈现数组中的每个元素。但是,每个单元格的默认 DataContext 是该行,即 data [x] 。因此,我需要通过将根可视元素的 DataContext 设置为绑定<$ c,来为每个列参数化 DataTemplate $ c> [y] 其中 y 是列索引。目前 DataTemplate 是在 DataGrid.Resources 中定义的,并由 FindResource()检索。 code>每次都返回相同的实例。除了调用 LoadContent()之外,还提供了 UIElement 树,而不是加载 VisualTree DataTemplate 本身上的c $ c>。我正在寻找一种在代码中实例化 DataTemplate 的方法,进行所需的修改并将其设置为 DataGridTemplateColumn.CellTemplate

I am displaying a 2-dimensional array data[][] in a DataGrid by adding DataGridTemplateColumn columns on my own and there is a DataTemplate defined in XAML that knows how to present each element in the array. However the default DataContext for each cell is the row, i.e. data[x]. So I need to "parameterize" the DataTemplate for each column by setting the root visual element's DataContext to binding "[y]" where y is the column index. Currently the DataTemplate is defined as in DataGrid.Resources and retrieved by FindResource() which is returning the same instance every time. Besides calling LoadContent() gives me the UIElement tree rather than loading the VisualTree on the DataTemplate itself. I am looking for a way to instantiate the DataTemplate in code, do the desired modification and set to DataGridTemplateColumn.CellTemplate.

推荐答案

受西西弗(Sisyphe)的回答启发,我发现了这种更可移植的解决方案:

Inspired by Sisyphe's answer, I found this more portable solution:

public class DataGridBoundTemplateColumn : DataGridTemplateColumn
{
    public string BindingPath { get; set; }

    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        var element = base.GenerateEditingElement(cell, dataItem);
        element.SetBinding(ContentPresenter.ContentProperty, new Binding(this.BindingPath));
        return element;
    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var element = base.GenerateElement(cell, dataItem);
        element.SetBinding(ContentPresenter.ContentProperty, new Binding(this.BindingPath));
        return element;
    }
}

用法:

var cellTemplate = (DataTemplate)this.dataGrid.FindResource("cellTemplate");
foreach (var c in data.Columns)
{
    var col = new DataGridBoundTemplateColumn
    {
        Header = c.HeaderText,
        CellTemplate = cellTemplate,
        BindingPath = string.Format("[{0}]", c.Index)
    };

    this.dataGrid.Columns.Add(col);
}

希望这对与我的问题有相同要求的人有所帮助。

Hope this helps someone who has the same requirement as the one in my question.

这篇关于为在代码中创建的DataGridTemplateColumn添加数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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