0

I have something similar to this and i want to change the binding for different instances.

In this example I would like to change the binding for myname to another (say hisname) in the second instance. Is this possible?

<Window.Resources>
    <DataTemplate x:Key="MyGridContentTemplate">
        <Grid Margin="8">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
 
            <TextBlock Text="Your name is:"/>
            <TextBlock Grid.Column="1" Margin="8,0,0,0"
                       FontWeight="Bold"
                       Text="{Binding MyName}"/>
 
            <TextBlock Grid.Row="1" Text="Your address is:"/>
            <TextBlock Grid.Row="1" Grid.Column="1" Margin="8,0,0,0"
                       FontWeight="Bold"
                       Text="{Binding Address}"/>
        </Grid>
    </DataTemplate>
</Window.Resources>
 
<Grid Margin="10">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
 
    <ContentPresenter Grid.Row="0" ContentTemplate="{StaticResource MyGridContentTemplate}" Content="{Binding}"/>
     
    <Separator Grid.Row="1"/>
 
    <ContentPresenter Grid.Row="2" ContentTemplate="{StaticResource MyGridContentTemplate}" Content="{Binding}"/>
</Grid>

Any help appreciated

Rektor
  • 9
  • 2

1 Answers1

0

Is this possible?

No, I am afraid. You cannot replace bindings or "override" only a part of a template. Unfortunately you must then (re)define the entire template as a whole.

You might want to consider creating the template programmatically.

mm8
  • 163,881
  • 10
  • 57
  • 88