One, find the element named registered in XAML
<Buttonx:name= "BTN1"Content= "Show content"HorizontalAlignment= "Left"Margin= "25,115,0,0"VerticalAlignment= "Top"Width= "a"Height= "$"Click= "Btn1_click"/> <TextBoxx:name= "Txtone"HorizontalAlignment= "Left"Height= "All"Margin= "25,62,0,0"textwrapping= "Wrap"Text= "TextBox"VerticalAlignment= "Top"Width= "a"/>
//use FindName () to findTextBox Txtone = This. FindName ("Txtone") asTextBox;if(Txtone! =NULL) {MessageBox.Show (txtone.text);//can access the}textbox Txttwo= Btn1. FindName ("Txtone") asTextBox;if(Txttwo! =NULL) {MessageBox.Show (txttwo.text);//You can also access the}
Second, find the background code named registered elements
Background Add Way
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);}
Note: The element name is registered and cannot be duplicated.
-----------reproduced in the following content: http://www.cnblogs.com/Clingingboy/archive/2010/11/29/1891253.html
Pre-set element name
WPF has two ways to set the name of an element
<StackPanelx:name= "Panel"> <LabelName= "Name1"Content= "Name1label"/> <Labelx: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 > < 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
<UserControlx: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> <ButtonHorizontalAlignment= "Left"VerticalAlignment= "Top"x:name= "BTN">UserControl Button</Button> </Grid></UserControl>
Using UserControl in the main form
<x:name= "Panel"> <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
New MyUserControl (); var New namescope (); this. 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
<ContentControlx:name= "CC"> <contentcontrol.contenttemplate> <DataTemplate> <ButtonHorizontalAlignment= "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
varNS = UC. GetValue (Namescope.namescopeproperty) asidictionary<string,Object>;varLocalns = This. GetValue (Namescope.namescopeproperty) asidictionary<string,Object>;foreach(varNinchNS) { 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