Solved

How to referance a datatemplate in WPF from c#

Posted on 2009-05-14
2
425 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

929 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