How to referance a datatemplate in WPF from c#

I have created a datatemplate in WPF and I would like to call it in C# how can I do this?  I have attempted a couple ways like
DataTemplate dt = System.Windows.Application.Current.Resources[datatemplate] as DataTemplate;

and while i try to build it says datatemplate is not in the current context.

WPF Code
         <DataTemplate x:Key="lbl" x:Name="datatemplate">
                        <!-- Grid 2x2 with black border  -->
                        <Border BorderBrush="Black">
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                <!-- x-coordinate -->
                                <TextBlock Text="X=" />
                 <TextBlock Grid.Column="1" Text="{Binding Path=[XValues]}" />
                 <!-- y-coordinate -->
                <TextBlock Grid.Row="1" Text="Y=" />
                 <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value}" />

Open in new window

Who is Participating?
unmeshdaveConnect With a Mentor Commented:
oops!! for datatemplate, you need to use DataTemplateSelector class. you need to override this class & implement selecttemplate method.  
did u try System.Windows.Application.Current.Resources.FindName method?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.