One, find the element named registered in XAML
<button x:name= "BTN1" content= "Show content" horizontalalignment= "left" margin= "25,115,0,0" verticalalignment= "Top" Width= "height=" "click=" Btn1_click "/> <textbox x:name=" Txtone "horizontalalignment=" left "Height=" " margin=" 25,62,0,0 "textwrapping=" Wrap "text=" TextBox "verticalalignment=" Top "width="/>
Use FindName () to find the textbox Txtone = this. FindName ("Txtone") as textbox;if (txtone! = null) { MessageBox.Show (txtone.text);//can be accessed to}textbox txttwo = btn1. FindName ("Txtone") as textbox;if (txttwo! = null) { MessageBox.Show (txttwo.text);//can also be accessed to}
Second, find the background code named registered elements
Background Add Way
TextBlock block = new TextBlock (); block. Text = "Sea Life Moon";p anelone.registername ("Block1", block); Register the block named Block1 name on the Panelone PANELONE.CHILDREN.ADD (block);
Obtained from the current window, can be accessed to TextBlock block1 = btn1. FindName ("Block1") as textblock;if (block1! = null) { MessageBox.Show (block1. Text);} Obtained from the panel, can be accessed to TextBlock Block2 = Panelone.findname ("Block1") as textblock;if (Block2! = null) { MessageBox.Show (" Panelone: "+block2. Text);}
Note: The element name is registered and cannot be duplicated.
Pre-set element name
WPF has two ways to set the name of an element
<stackpanel x:name= "Panel" > <label name= "name1" content= "Name1label"/> <label x:name= "name2 "Content=" Name2label "/> </StackPanel>
The point here is not to discuss the difference between name and x:name,
Name is a property on a real element, and x:name the Magic of XAML (parsing), what we see is only appearances.
Not just the name of the registered element
In addition to element, other types are also available, such as
<Label> <Label.BorderBrush> <solidcolorbrush x:name= "Brushname" ></solidcolorbrush > </Label.BorderBrush> </Label>
Some basic types cannot be set in XAML, such as the String,int type. But can be set by code
This. Registername ("str", "Hello");
There are only functional examples, but in practice do not do so, not in itself designed for this.
Find elements of UserControl
First define a UserControl
<usercontrol x:class= "Namescopedemo.myusercontrol" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation " xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml " xmlns:mc="/http/ schemas.openxmlformats.org/markup-compatibility/2006 " xmlns:d=" http://schemas.microsoft.com/expression/ blend/2008 " mc:ignorable=" D " d:designheight=" "d:designwidth=" > <Grid> < Button horizontalalignment= "left" verticalalignment= "Top" x:name= "btn" >usercontrol button</button> </Grid></UserControl>
Using UserControl in the main form
<stackpanel x:name= "Panel" > <local:myusercontrol x:name= "MyControl" ></local:MyUserControl> </StackPanel>
Now find the results as follows
If you can't find btn using the main form, you can do it through UserControl.
The UI was sliced up in design, but it caused trouble finding elements.
Named range (namescope)
If you are using code, the method of adding is as follows
UC = new MyUserControl (); var ns = new namescope (); Registername ("MyControl", UC);p Anel. Children.add (UC);
A separate named range is created for UserControl, and the elements that want to find MyUserControl can be found by MyUserControl level FindName.
Template fetching elements
<contentcontrol x:name= "CC" > <ContentControl.ContentTemplate> <DataTemplate> < Button horizontalalignment= "left" verticalalignment= "Top" x:name= "btn" >usercontrol button</button> </DataTemplate> </ContentControl.ContentTemplate> </ContentControl>
See the above code, CC cannot find through FindName to btn, only the root element of the template can be, this root element is generally contentpresenter, so when looking for an element within a template, you must tell its root element
Public Object FindName (string name,frameworkelement templatedparent)
So first we have to find ContentPresenter, and only through the visual tree above looking, overall is still more troublesome, do not know why the internal API does not encapsulate.
Registering an element name within a template to a parent
var ns = UC. GetValue (Namescope.namescopeproperty) as idictionary<string, Object>;var localns = this. GetValue (Namescope.namescopeproperty) as idictionary<string, Object>;foreach (var n in ns) { if ( Localns.containskey (N.key)) {this . Registername (N.key, N.value);} }
First get the current element's namescope, and then register it to the parent named range for easy looking, it is worth noting that some name inside the template is fixed, such as ScrollBar, its internal template element name name is specified, so do not register it.
WPF FindName () find named registered elements