简体   繁体   English

DataGrid的自定义列模板

[英]Custom column template for DataGrid

I have a DataGrid bound to a DataTable.DefaultView, which renders the grid using auto generation of columns. 我有一个绑定到DataTable.DefaultView的DataGrid,它使用自动生成列来呈现网格。 That part works fine. 那部分工作正常。 However, for some columns I would like to use a custom template. 但是,对于某些列,我想使用自定义模板。 The problem is that columns in the table change on each load, so the solution needs to be generic. 问题在于表中的列在每次加载时都会发生变化,因此解决方案需要通用。

I can hook into the AutoGeneratingColumn event as described here , but still have problem with defining the template binding: 我可以按此处所述插入AutoGeneratingColumn事件,但是在定义模板绑定时仍然存在问题:

<UserControl.Resources>
    <DataTemplate x:Key="customCellTemplate">
        <TextBlock Text="{Binding ???"></TextBlock>
    </DataTemplate>
</UserControl.Resources>

(...)

<DataGrid ItemsSource="{Binding DefaultView}" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn">
</DataGrid>

And my code behind: 我的代码后面:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    string colName = e.PropertyName;
    if (someCondition)
    {
        var templateColumn = new DataGridTemplateColumn();
        templateColumn.Header = colName;
        templateColumn.CellTemplate = (DataTemplate)Resources["customCellTemplate"];
        templateColumn.SortMemberPath = colName;
        e.Column = templateColumn;
     }

As you can see I don't know how to define the binding in the column template, because the column name changes. 如您所见,我不知道如何在列模板中定义绑定,因为列名会更改。

EDIT: 编辑:

In addition to the accepted answer - sometimes it's easier to create the entire template programmatically as described here: http://fczaja.blogspot.com/2013/12/wpf-datagrid-custom-template-for.html 除了可接受的答案-有时,按如下所述通过程序创建整个模板也更容易: http : //fczaja.blogspot.com/2013/12/wpf-datagrid-custom-template-for.html

Using a StaticResource forces you to keep it the same -- remember, static means there's just one instance, so if you change its binding for one column, you will change it for all of them. 使用StaticResource强制您保持不变-请记住,static意味着只有一个实例,因此,如果您为一列更改其绑定,则将为所有列更改它。 So it will have to be like this: 因此它必须是这样的:

<DataTemplate x:Key="customCellTemplate">
    <TextBlock Text="{Binding}"></TextBlock>
</DataTemplate>

I was thinking you could use this template in a dynamic way by wrapping it in another DataTemplate using a ContentControl . 我想您可以通过使用ContentControl将其包装在另一个DataTemplate中来以动态方式使用此模板。 Set the Content property dynamically, and use the static template for the ContentTemplate : 动态设置Content属性,并为ContentTemplate使用静态模板:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    string colName = e.PropertyName;
    if (someCondition)
    {
        string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><ContentControl Content=""{0}"" ContentTemplate=""{1}"" /></DataTemplate>";
        var tmpl = (DataTemplate)XamlReader.Load(string.Format(xaml, "{Binding " + colName + "}", "{StaticResource customCellTemplate}"));
        var templateColumn = new DataGridTemplateColumn();
        templateColumn.CellTemplate = tmpl;
        templateColumn.Header = colName;
        templateColumn.SortMemberPath = colName;
        e.Column = templateColumn;
     }
}

The only catch is that, with this setup, I believe "customCellTemplate" will have to be defined at the application level. 唯一要注意的是,通过这种设置,我相信必须在应用程序级别定义“ customCellTemplate”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM