在 WPF DataGrid 自动生成的列中使用 CellTemplateSelector 强制使用 DataTemplateCell

发布于 2024-12-27 11:23:34 字数 1727 浏览 1 评论 0原文

我有一个数据网格,我将数据表绑定到该数据网格。我不知道数据表中将包含哪些行或列,因此我将数据网格的 AutogenerateColumns 属性设置为 true。我唯一确定的是,数据表中的每个单元格都属于 Field 类型,并且 Field 类有一个名为 Type 的枚举属性。

<DataGrid
    x:Name="dg"
    AutoGenerateColumns="True"
    ItemsSource="{Binding Path=Fields}"
    AutoGeneratingColumn="dg_AutoGeneratingColumn">
</DataGrid>

我想要做的是强制所有自动生成的列都是 DataTemplateColumns,其中 CellTemplateSelector 属性设置为 FieldCellTemaplateSelector 对象。为此,我在 AutoGenerateColumn 事件中添加以下代码:

private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    //cancel the auto generated column
    e.Cancel = true;

    //create a new template column with the CellTemplateSelector property set
    DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
    dgtc.CellTemplateSelector = new FieldCellTemplateSelector();
    dgtc.IsReadOnly = true;
    dgtc.Header = e.Column.Header;

    //add column to data grid
    DataGrid dg = sender as DataGrid;
    dg.Columns.Add(dgtc);
}

FieldCellTemplateSelector 类的代码如下:

public class FieldCellTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return base.SelectTemplate(item, container);
    }
}

在 SelectTemplate 方法中,我需要获取单元格中包含的 Field 对象,并根据 Type 返回相关的数据模板该字段的属性。问题是传递的 item 参数不是 Field 类型,而是 DataRowView 类型。

我可以通过执行以下操作来获取 DataGridCell 对象:

ContentPresenter presenter = container as ContentPresenter;
DataGridCell cell = presenter.Parent as DataGridCell;

但是,单元格的数据上下文也是 DataRowView 类型。我的领域发生了什么事?它消失了吗?任何人都可以让我知道如何解决这个问题,或者如何解决这个问题,

提前致谢。

I have a data grid to which I bind a DataTable. I don't know what rows or columns will be in the data table so I set the AutogenerateColumns property of the data grid to true. The only thing I do know for certain is that every cell in the data table will be of type Field and the Field class has a enum property called Type.

<DataGrid
    x:Name="dg"
    AutoGenerateColumns="True"
    ItemsSource="{Binding Path=Fields}"
    AutoGeneratingColumn="dg_AutoGeneratingColumn">
</DataGrid>

What I want to do is to force all of the auto generated columns to be DataTemplateColumns which have the CellTemplateSelector property set to a FieldCellTemaplateSelector object. To do this I add the following code the AutoGeneratingColumn event:

private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    //cancel the auto generated column
    e.Cancel = true;

    //create a new template column with the CellTemplateSelector property set
    DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
    dgtc.CellTemplateSelector = new FieldCellTemplateSelector();
    dgtc.IsReadOnly = true;
    dgtc.Header = e.Column.Header;

    //add column to data grid
    DataGrid dg = sender as DataGrid;
    dg.Columns.Add(dgtc);
}

The code for the the FieldCellTemplateSelector class is as follows:

public class FieldCellTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return base.SelectTemplate(item, container);
    }
}

In the SelectTemplate method I need to get the Field object that is contained in the cell and return the relevant data template based on the Type property of that field. The problem is that the item parameter that is passed is not of type Field, it is of type DataRowView.

I can get the DataGridCell object by doing the following:

ContentPresenter presenter = container as ContentPresenter;
DataGridCell cell = presenter.Parent as DataGridCell;

However, the data context of the cell is also of type DataRowView. What has happened to my Field? Has it disappeared? Can anyone let me know how to get at it or how I can achieve a solution to this problem

Thanks in advance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文