Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Common DataGridTemplateColumn in WPF

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>

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

like image 719
Jagan Avatar asked Apr 28 '12 06:04

Jagan


1 Answers

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>

Using

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTemplateColumn 
                 CellEditingTemplate="{StaticResource CellEdintingTemplate}" 
                 CellTemplate="{StaticResource CellTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
like image 70
asktomsk Avatar answered Nov 15 '22 06:11

asktomsk