Search code examples
c#xamlbindingdatatemplate

How to edit/use xaml datatemplate in code


In XAML, I have:

<DataTemplate x:Key="AgeItemTemplate">
    <Border BorderThickness="0,0,0,0" BorderBrush="#6FBDE8">
           <TextBlock Margin="2" Text="{Binding Age}" VerticalAlignment="Center" Grid.Column="1" />
     </Border>
</DataTemplate>

How could I use that DataTemplate in code?

enter image description here

I know I can create a new template and linked to a gridview column but I want to define that template in xaml. Is there any way to modify and use that dataTemplate in code behind?


Solution

  • You need to use the findresource method on FrameworkElement.