#95 – x:Name vs. Name
October 15, 2010 3 Comments
You’ll typically see a Name property on XAML elements. This property can be used in your code-behind, as a reference to the object being created.
<Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" /> <Button Content="Button" Height="23" Name="button2" Width="75" />
But you might also see x:Name being used (attribute syntax) in XAML.
<Viewport3D Name="viewport1"> <ModelVisual3D x:Name="visual1"> </ModelVisual3D> </Viewport3D>
The Name property can be used with elements that inherit a Name property from their base class (e.g. FrameworkElement). But for classes that don’t define a Name property or inherit from a class that does, you must use the x:Name property if you want to reference the object from your code-behind.
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.