Solved

WPF -  Template.FindName  - Can't Find

Posted on 2011-03-22
3
2,814 Views
Last Modified: 2013-11-12
I have the following template

     <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type FlowDocumentReader}">
                 
 <Button x:Name="btnPrint" Height="25" Width="70" ToolTip="Print"  Margin="5,0,0,0" >
                 
But when I run my code to find the buttonl... it's always returns null ..

  Button btnPrint  = (Button)this.flowdocReader.Template.FindName("btnPrint",this.flowdocReader);
0
Comment
Question by:JElster
[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
3 Comments
 
LVL 3

Accepted Solution

by:
politex earned 500 total points
ID: 35196148
Hi, you are sure what a FlowDocumentReader is loaded? sample:
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var a = fdr.Template.FindName("bbb", this.fdr);
            //a =null
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var a = fdr.Template.FindName("bbb", this.fdr);
            //a = bbb
        }

        private void fdr_Loaded(object sender, RoutedEventArgs e)
        {
            var a = fdr.Template.FindName("bbb", this.fdr);
            //a = bbb
        }
}

Open in new window

0
 
LVL 11

Expert Comment

by:saragani
ID: 35196175
Ok, lets start with the basic question... why are you trying to access your button in Code-Behind instead of using Binding, Commands, MVVM and god knows what.

Where must be a better what to do what you are doing (which I don't know what exactly).
0
 
LVL 1

Author Comment

by:JElster
ID: 35197668
I had my code in  OnApplyTemplate()
Which previous articles mentioned... but it does Not Find it.
It DOES in the Window_Loaded()
thanks..


I've been trying to every option to wire up a style/template button to a page.
I'm new to all this...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

737 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