locked
What is the difference between the x:Name and Name property? RRS feed

  • Question

  • Since I need to learn WPF I thought I'd take a different approach to using the technology and actually seek to understand it.  In short, there are dark days ahead for all of us.  =0)

    So far my understanding is that 'x' in the OP title is just shorthand for a reference.
    The reference looks like this:

    <Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    The shorthand for this reference could be anything.
    It could be 'dog', 'cat', or 'billgates'.

    <Window xmlns:dog="http://schemas.microsoft.com/winfx/2006/xaml">

    I'm reading in Windows Presentation Foundation Unleased (SAMS) and it says that when creating a button element you could declare it one of two different ways:

    <Button x:Name="okButton">OK</Button>

    OR

    <Button Name="okButton">OK</Button>

    Elements which subclass FrameworkElement and FrameworkContentElement may use the "Name" property, according to the text.   Using "Name" makes it easier to reference the item in procedural code, which is nice.

    What I'd like to understand is - what is actually different about these different ways of applying a name to the element. 

    My theory is that the x:Name is pulling the definition of the property from the schema (which doesn't allow it to be available in code) and the Name is pulling the definition of the property from the actual base class.   Would that be the correct English language way to say that?

    Monday, July 20, 2009 12:32 PM

Answers

  • There is no difference in your button example. Name is a shorthand for x:Name and you only really need x:Name when Name is ambiguous.
    • Proposed as answer by Bruce.Zhou Wednesday, July 22, 2009 6:23 AM
    • Marked as answer by Bruce.Zhou Friday, July 24, 2009 8:19 AM
    Monday, July 20, 2009 12:41 PM
  • http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q16.4

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
    MSBuild user? Try out the DynamicExecute task in the MSBuild Extension Pack source; it's currently in Beta so get your comments in!
    • Proposed as answer by Bruce.Zhou Wednesday, July 22, 2009 6:23 AM
    • Marked as answer by Bruce.Zhou Friday, July 24, 2009 8:19 AM
    Monday, July 20, 2009 12:42 PM
  • They should be identical.  We have an attribute called the RuntimeNamePropertyAttribute which tells the XAML parser to treat that property exactly the same way as x:Name.  So if you set, x:Name, we'll set the RuntimeNameProperty as well.  FE & FCE in WPF both have Name as their RuntimeNameProperty
    • Proposed as answer by Bruce.Zhou Wednesday, July 22, 2009 6:23 AM
    • Marked as answer by Bruce.Zhou Friday, July 24, 2009 8:19 AM
    Monday, July 20, 2009 2:36 PM

All replies

  • There is no difference in your button example. Name is a shorthand for x:Name and you only really need x:Name when Name is ambiguous.
    • Proposed as answer by Bruce.Zhou Wednesday, July 22, 2009 6:23 AM
    • Marked as answer by Bruce.Zhou Friday, July 24, 2009 8:19 AM
    Monday, July 20, 2009 12:41 PM
  • http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q16.4

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
    MSBuild user? Try out the DynamicExecute task in the MSBuild Extension Pack source; it's currently in Beta so get your comments in!
    • Proposed as answer by Bruce.Zhou Wednesday, July 22, 2009 6:23 AM
    • Marked as answer by Bruce.Zhou Friday, July 24, 2009 8:19 AM
    Monday, July 20, 2009 12:42 PM
  • They should be identical.  We have an attribute called the RuntimeNamePropertyAttribute which tells the XAML parser to treat that property exactly the same way as x:Name.  So if you set, x:Name, we'll set the RuntimeNameProperty as well.  FE & FCE in WPF both have Name as their RuntimeNameProperty
    • Proposed as answer by Bruce.Zhou Wednesday, July 22, 2009 6:23 AM
    • Marked as answer by Bruce.Zhou Friday, July 24, 2009 8:19 AM
    Monday, July 20, 2009 2:36 PM