?
Solved

How to referance a datatemplate in WPF from c#

Posted on 2009-05-14
2
Medium Priority
?
440 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

801 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