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