Wpf – x: meaning in xaml

wpfxaml

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

Best Answer

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.

Related Topic