Solved

How to referance a datatemplate in WPF from c#

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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