且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在WPF中创建Common DataGridTemplateColumn

更新时间:2023-10-04 23:48:28

您不能直接模板 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>