Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>x:Name and Name are referencing different namespaces. </p> <p><strong>x:name</strong> is a reference to the x namespace defined by default at the top of the Xaml file.</p> <pre><code>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" </code></pre> <p>Just saying <strong>Name</strong> uses the default below namespace. </p> <pre><code>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" </code></pre> <p><strong>x:Name</strong> is saying use the namespace that has the <strong>x</strong> alias. x is the default and most people leave it but you can change it to whatever you like</p> <pre><code>xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml" </code></pre> <p>so your reference would be <strong>foo:name</strong></p> <p><a href="http://windowsclient.net/learn/video.aspx?v=31429" rel="nofollow noreferrer">Define and Use Namespaces in WPF</a></p> <hr> <p>OK lets look at this a different way. Say you drag and drop an button onto your Xaml page. You can reference this 2 ways <strong>x:name</strong> and <strong>name</strong>. All <em>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</em> <strong>and</strong> <em>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"</em> are is references to multiple namespaces. Since <strong>xaml</strong> holds the <strong>Control</strong> namespace(not 100% on that) and <strong>presentation</strong> holds the <strong>FrameworkElement</strong> AND the <strong>Button class</strong> has a inheritance pattern of: </p> <pre><code>Button : ButtonBase ButtonBase : ContentControl, ICommandSource ContentControl : Control, IAddChild Control : FrameworkElement FrameworkElement : UIElement, IFrameworkInputElement, IInputElement, ISupportInitialize, IHaveResources </code></pre> <p>So as one would expect anything that inherits from FrameworkElement would have access to all its public attributes. so in the case of Button it is getting its Name attribute from FrameworkElement, at the very top of the hierarchy tree. <strong>So</strong> you can say <strong>x:Name</strong> or <strong>Name</strong> and they will both be accessing the getter/setter from the FrameworkElement. </p> <p><a href="http://msdn.microsoft.com/en-us/library/ms747086.aspx" rel="nofollow noreferrer">MSDN Reference</a></p> <p>WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The <strong>XmlnsDefinitionAttribute</strong> attribute is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces, such as System.Windows and System.Windows.Controls, to the <a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation" rel="nofollow noreferrer">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a> namespace.</p> <p>So the assembly attributes will look something like:</p> <p>PresentationFramework.dll - XmlnsDefinitionAttribute:</p> <pre><code>[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")] [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Data")] [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Navigation")] [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shapes")] [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Documents")] [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload