Solved

How to referance a datatemplate in WPF from c#

Posted on 2009-05-14
2
422 Views
Last Modified: 2013-11-12
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.
thanks


WPF Code
 

<Window.Resources>

         <DataTemplate x:Key="lbl" x:Name="datatemplate">
 

                        <!-- Grid 2x2 with black border  -->

                        <Border BorderBrush="Black">
 

                            <Grid>
 

                                <Grid.ColumnDefinitions>

                                    <ColumnDefinition />

                                    <ColumnDefinition />

                                </Grid.ColumnDefinitions>
 

                                <Grid.RowDefinitions>

                                    <RowDefinition />

                                    <RowDefinition />

                                </Grid.RowDefinitions>
 

                                <!-- 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}" />

 

          </Grid>

                        </Border>

                    </DataTemplate>

    </Window.Resources>

Open in new window

0
Comment
Question by:perrytaylor
  • 2
2 Comments
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24390760
did u try System.Windows.Application.Current.Resources.FindName method?
0
 
LVL 8

Accepted Solution

by:
unmeshdave earned 500 total points
ID: 24396475
oops!! for datatemplate, you need to use DataTemplateSelector class. you need to override this class & implement selecttemplate method.
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.selecttemplate.aspx  
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now