WPF FindName () did not find the element with the specified name

Source: Internet
Author: User

1.FindName () description, which can be used to get an element or label with a registered name

 // //  summary:  //  Find the element with the provided identifier name.  // //  parameters:  //  name:  //  The name of the requested element.  // //  Returns the result:  //  The requested element. If no matching element is found, the value will be null.  public  object  FindName (string  name); 

But the way to add a name registration in the background is to use Registername () instead of assigning a value directly to name

        //        //Summary://provides an accessor to simplify access to the System.Windows.NameScope registration method. //        //Parameters://Name://the name to use in the specified name-object map. //        //scopedelement://the mapped object.          Public voidRegistername (stringNameObjectScopedelement);

To illustrate:

1. Direct assignment name, cannot be found by FindName ()

 //  background Add the element of name  Label LBL1 = new   Label (); lbl1. Content  =   Zhang Sanfeng   " ;LBL1. Name  =  lbl1  "; //  This specifies that the  panelOne.Children.Add (LBL1) cannot be found by FindName (); 
// find elements added in background, not found this. FindName ("lbl1" as Label; if NULL ) {    MessageBox.Show (lbl1. Content.tostring ());}

2. Using Registername (), registration can be found using FindName ()

New" Sea Bright moon ";p anelone.registername ("block1 " // Register the block named Block1 name on the Panelone PANELONE.CHILDREN.ADD (block);
//obtained from the current window, you can access theTextBlock Block1 = btn1. FindName ("Block1") asTextBlock;if(Block1! =NULL) {MessageBox.Show (block1. Text);}//obtained from the panel, you can access theTextBlock Block2 = Panelone.findname ("Block1") asTextBlock;if(Block2! =NULL) {MessageBox.Show ("Panelone:"+Block2. Text);}

WPF FindName () did not find the element with the specified name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.