Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x: meaning in xaml

Tags:

wpf

xaml

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

like image 317
Night Walker Avatar asked Jan 01 '11 10:01

Night Walker


People also ask

What is X key in XAML?

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.

What is X type in WPF?

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.

What is the difference between name and X name in XAML?

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.

What is xmlns X?

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.


1 Answers

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.

like image 174
Dabblernl Avatar answered Oct 12 '22 20:10

Dabblernl