Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, WPF experts:

<textbox x:name="textBox1"> some text</textbox>
what does it mean here "x:", why we need it here?

thanks!
Posted
Updated 4-Sep-12 9:42am
v2

From the documentation[^]

Specifies a run-time object name for the instance that exists in run-time code after an object element is processed
 
Share this answer
 
Comments
Kenneth Haugland 4-Sep-12 15:52pm    
Ah, Microsoft has documentation? 5'ed :)
[no name] 4-Sep-12 16:08pm    
Thanks. Imagine the possibilites if everyone had documentation....
Kenneth Haugland 4-Sep-12 16:45pm    
Russian langauge has documentation, but that does not help me :laugh:
See this:
http://social.msdn.microsoft.com/Forums/da-DK/wpf/thread/216553bf-9be2-4d0a-acc1-b38af8ef948e[^]

Quote:
If it was "Key" rather than "x:Key" this would require "Key" to be an "attribute" of
the "element" added as a resource, according to the syntax rules of XAML and even XML.

In WPF this would mean that every object added as a resource
would need to expose a writable DependencyProperty named "Key".

This would rule out all types that are not DependencyObjects - such as strings or URIs or
ViewModels instances - and more over all those that don't have such DP named "Key",
so merely all objects commonly used as resources!

With the implementation as "x:Key" directive the key doesn't need to be a true attribute of
the element added, you can look it as kind of an "attached" attribute to the element (just allegorically
not technically an AttachedProperty).
Note that if a type has a property marked by the DictionaryKeyPropertyAttribute then you
actually don't need the "x:Key" but may rather use the "Key" property of that type (which may have a different name) - just the way you proposed.

Note that for "x:Name" and "Name" the relation is about the same as for "x:Key" and "Key".
If you use "Name" the element needs to expose a writable DP named "Name",
which in contrast to "Key" is most often the case - because "Name" is a DP of
every FrameworkElement and FrameworkContentElement, so >90% of elements used in XAML,
so that's why Name and x:Name often appear as synonyms and mutually interchangeable
(which they are not fully) but "x:Key" and "Key" do not.


The same applies to all x: attributtes in XAML
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900