0

In Windows Phone xaml page, some element has x:Name

<TextBlock x:Name="PageTitle" Text="simple" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

While others have Name

<TextBlock Name="tbSource" Height="30" HorizontalAlignment="Left" Margin="129,178,0,0"  Text="Source" VerticalAlignment="Top" Width="304" />

What is the difference ?

onmyway133
  • 45,645
  • 31
  • 257
  • 263
  • possible duplicate. see [here](http://stackoverflow.com/questions/589874/in-wpf-what-are-the-differences-between-the-xname-and-name-attributes) – itsho Oct 07 '12 at 00:11

2 Answers2

0

Not sure, but I think x:Name elements are accessible in code by their name. e.g. PageTitle.Text = "some text". And "Name" is just a regular property of an element. Please also see the following article about x:Name directive.

David
  • 72,686
  • 18
  • 132
  • 173
0

The x: part is a namespace alias. It tells the compiler where the property exists. In this case it's pointing to the default namespace for XAML.
The Name property is part of System.Object which is in that namespace.
When you include x:Name to identify a property called Name in XAML what you're explicitly saying is that you're referring to the Name property of the underlying System.Object. As all objects inherit from this it's accessible to all classes.

Why might any of this matter?
If you overrode the Name property in a class then specifying the namespace will allow you to be explicit about which version of Name you're referring to.
Side note: you will save yourself a lot of pain by never overriding Name.

Does it matter which you use?
No. As long as Name is never overridden and you need to refer to the specific version of it.

It's probably in your interest to be consistent in whether you use it or not. Most people do (as does VS when it creates controls for you) so it's probably easiest to keep them there.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143