Aziz Alzayed
Aziz Alzayed

Reputation: 236

WPF - How can I make generic binding for DataTemplate?

I've tried to search on the internet for an idea to solve my problem but I didn't get any.

I have these DataTemplates:

<DataTemplate x:Key="UnitGridCell">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
        <dxe:TextEdit EditValue="{Binding Row.Unit.NewValue}" Style="{StaticResource PartStyle}"  />
        <dxe:TextEdit  EditValue="{Binding Row.Unit.OldValue}" Style="{StaticResource PartStyle}" />
    </StackPanel>
</DataTemplate>
<DataTemplate x:Key="PreGridCell">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
        <dxe:TextEdit EditValue="{Binding Row.Pre.NewValue}" Style="{StaticResource PartStyle}"  />
        <dxe:TextEdit  EditValue="{Binding Row.Pre.OldValue}" Style="{StaticResource PartStyle}" />
    </StackPanel>
</DataTemplate>
<DataTemplate x:Key="ExecutionGridCell">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
        <dxe:TextEdit EditValue="{Binding Row.Execution.NewValue}" Style="{StaticResource PartStyle}"  />
        <dxe:TextEdit  EditValue="{Binding Row.Execution.OldValue}" Style="{StaticResource PartStyle}" />
    </StackPanel>
</DataTemplate>

for these GridColumns:

 <Grid >
    <dxg:GridControl ItemsSource="{Binding DataGridModels}" >
    <dxg:GridColumn x:Name="UnitCol" FieldName="Unit"  CellTemplate="{StaticResource UnitGridCell}"/>
    <dxg:GridColumn x:Name="PreCol" FieldName="Pre"  CellTemplate="{StaticResource PreGridCell}"/>
    <dxg:GridColumn x:Name="ExCCol" FieldName="Execution" CellTemplate="{StaticResource ExecutionGridCell}"/>
                

and the question is : how can I make a generic binding so I can make one DataTemplate for all grid columns?

And thank you in advance !

Upvotes: 0

Views: 221

Answers (1)

mm8
mm8

Reputation: 169390

I am afraid you cannot reuse the same template and only replace the binding path in XAML.

A template must be defined as a whole.

You may consider to create the templates programmtically using the XamlReader.Parse method but there is no markup (XAML only) solution.

Using XamlReader.Parse will let you use a method like string.Replace to replace the binding path for each column. You will still have to create a template per column but it will let you define the XAML markup for the template once in a string variable or similar.

Upvotes: 1

Related Questions