Solved

WPF -  Template.FindName  - Can't Find

Posted on 2011-03-22
3
2,583 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now