Solved

How to referance a datatemplate in WPF from c#

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

785 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