6

For instance, I have this code:

<Grid>
    <Rectangle Name="TheRectangle" Fill="AliceBlue" Height="100" Width="100">            
    </Rectangle>
</Grid>

VS.

<Grid>
    <Rectangle x:Name="TheRectangle" Fill="AliceBlue" Height="100" Width="100">            
    </Rectangle>
</Grid>

Thank you very much for the information. I'm very excited about learning something new like this. :D

Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254

2 Answers2

10

wpfwiki

There is basically no difference between the two.

The "x:Name" expression is used in XAML to assign a name to an object that will be used to access the object from the code-behind.

Many classes of the framework expose a Name property, which does exactly this. For these classes, both x:Name and the Name property can be used interchangeably.

serge_gubenko
  • 20,186
  • 2
  • 61
  • 64
  • 1
    One minor difference: Name cannot (for some crazy reason) be used in XAML on a control that is declared in the same assembly as the XAML file. In that scenario, you *must* use x:Name instead. – itowlson Nov 11 '09 at 23:56
  • So it's better for me to be safe and make it a habbit of always using x:Name in the case a framework class doesn't haev a Name property? Regards. – Sergio Tapia Nov 12 '09 at 00:03
0

You will have a response here

Basically, x:Name is used by WPF to be accessed in Runtime and by XAML to generate fields in the code behind.

Community
  • 1
  • 1
Zied
  • 1,696
  • 12
  • 20