Solved

How to referance a datatemplate in WPF from c#

Posted on 2009-05-14
2
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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