I see a lot statements like
<TextBox x:Name="txtInput" />
or like
<BooleanToVisibilityConverter x:Key="boolToVis" />
Why the x:
is needed and what it gives me.
<DockPanel.Resources> <c:MyData x:Key="myDataSource"/> </DockPanel.Resources>
And here we have also the c:
Thanks for help
Uniquely identifies elements that are created and referenced in a XAML-defined dictionary. Adding an x:Key value to a XAML object element is the most common way to identify a resource in a resource dictionary, for example in a WPF ResourceDictionary.
WPF supports techniques that enable specifying the value of some properties of type Type without requiring an x:Type markup extension usage. Instead, you can specify the value as a string that names the type. Examples of this are ControlTemplate. TargetType and Style.
In general, Name and x:Name are interchangeable. The former is an actual property on the class and the latter is a directive that comes from the default x: namespace and is used by the XAML parser.
The xmlns:x attribute indicates an additional XAML namespace, which maps the XAML language namespace http://schemas.microsoft.com/winfx/2006/xaml. This usage of xmlns to define a scope for usage and mapping of a name scope is consistent with the XML 1.0 specification.
It is nothing more than shortcuts to the different namespaces for XML. You can choose them as you like. If you look at the upper lines in your XAML you will find the line:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Change the 'x
' to 'wpf
' for instance and you will see that you need to change all the 'x:
' prefixes in your code to 'wpf:
' to make it compile.
The 'c:
' prefix references code of your own. Say you have a class library that compiles to MyLib.dll. This library contains a class named MyData. To be able to reference the MyData class you need something like:
xmlns:c="clr-namespace:MyClasses;assembly=MyLib"
in your XAML header.
You can then reference the MyData class in you XAML with c:MyData
. But you are entirely free to change the 'c
' to 'myfabulousclasses
' or anything else you fancy.
The purpose of this? To distinguish classes or members that have the same name, but belong to different dll's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With