在WPF中创建Common DataGridTemplateColumn [英] Create Common DataGridTemplateColumn in WPF

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

问题描述

我需要创建一个常见的 DataGridTemplateColumn ,以便可以在具有不同对象和属性的应用程序中使用它。

I need to create a common DataGridTemplateColumn, so that I can use it across my application with different objects and properties.

这是一些示例代码,我在我的项目中使用

here is some sample code, I use in my project

<DataGridTemplateColumn Width="100*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Age}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>   
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=Age}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

我需要代码的通用版本,以便可以放置 DataTemplate app.xaml 中并在我的代码中引用它

I need a generic version of the code so that I can place the DataTemplate in app.xaml and reference it in my code

推荐答案

您不能直接模板 DataGridTemplateColumn 。但是幸运的是,您可以为单元格使用全局模板。看一个例子:

You can't template DataGridTemplateColumn directly. But fortunately you can use global templates for cells. Take a look at example:

App.xaml

<Application.Resources>

    <DataTemplate x:Key="CellEdintingTemplate">
        <TextBox Text="{Binding Path=Age}" />
    </DataTemplate>
    <DataTemplate x:Key="CellTemplate">
        <TextBlock Text="{Binding Path=Age}" />
    </DataTemplate>

</Application.Resources>

使用

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTemplateColumn 
                 CellEditingTemplate="{StaticResource CellEdintingTemplate}" 
                 CellTemplate="{StaticResource CellTemplate}" />
        </DataGrid.Columns>
    </DataGrid>

这篇关于在WPF中创建Common DataGridTemplateColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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