在WPF DataGrid自动生成列中使用CellTemplateSelector强制DataTemplateCell [英] Force DataTemplateCell with CellTemplateSelector in WPF DataGrid Autogenerated columns

查看:357
本文介绍了在WPF DataGrid自动生成列中使用CellTemplateSelector强制DataTemplateCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我绑定DataTable的数据网格。我不知道数据表中的行或列是什么,所以我将数据网格的AutogenerateColumns属性设置为true。我唯一知道的一点是,数据表中的每个单元格都将是Field类型,Field类有一个名为Type的枚举属性。

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

我想做的是强制所有自动生成的列为具有CellTemplateSelector的DataTemplateColumn属性设置为FieldCellTemaplateSelector对象。为此,我添加以下代码AutoGeneratingColumn事件:

  private void dg_AutoGeneratingColumn(object sender,DataGridAutoGeneratingColumnEventArgs e)
{
//取消自动生成的列
e.Cancel = true;

//使用CellTemplateSelector属性设置新的模板列
DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
dgtc.CellTemplateSelector = new FieldCellTemplateSelector();
dgtc.IsReadOnly = true;
dgtc.Header = e.Column.Header;

//添加列到数据网格
DataGrid dg =发送方作为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作为ContentPresenter; 
DataGridCell cell = presenter.Parent as DataGridCell;

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



提前感谢。

解决方案

我有同样的问题。在这个链接找到答案。



http://social.msdn.microsoft.com/Forums/en/wpf/thread/8b2e94b7-3c44-4642-8acc-851de5285062


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.

解决方案

I had the same issue. Found the answer at this link.

http://social.msdn.microsoft.com/Forums/en/wpf/thread/8b2e94b7-3c44-4642-8acc-851de5285062

这篇关于在WPF DataGrid自动生成列中使用CellTemplateSelector强制DataTemplateCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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