Solved

WPF -  Template.FindName  - Can't Find

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Resolve Dependency Issues 4 47
Replace &lt; with < 14 56
C# HTTP GET method sample code 3 41
VS2015 Redefinition errors 4 29
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

896 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

13 Experts available now in Live!

Get 1:1 Help Now