In WPF, what are the differences between the x:Name and Name attributes?

前端 未结 15 1281
闹比i
闹比i 2020-11-22 05:19

The title says it all. Sometimes it seems that the Name and x:Name attributes are interchangeable.

So, what are the definitive differences

相关标签:
15条回答
  • 2020-11-22 05:40

    X:Name can cause memory issues if you have custom controls. It will keep a memory location for the NameScope entry.

    I say never use x:Name unless you have to.

    0 讨论(0)
  • 2020-11-22 05:40

    My research is x:Name as global variable. However, Name as local variable. Does that mean x:Name you can call it anywhere in your XAML file but Name is not.
    Example:

    <StackPanel>
    <TextBlock Text="{Binding Path=Content, ElementName=btn}" />
    <Button Content="Example" Name="btn" />
    </StackPanel>
    <TextBlock Text="{Binding Path=Content, ElementName=btn}" />
    

    You can't Binding property Content of Button with Name is "btn" because it outside StackPanel

    0 讨论(0)
  • 2020-11-22 05:42

    Name:

    1. can be used only for descendants of FrameworkElement and FrameworkContentElement;
    2. can be set from code-behind via SetValue() and property-like.

    x:Name:

    1. can be used for almost all XAML elements;
    2. can NOT be set from code-behind via SetValue(); it can only be set using attribute syntax on objects because it is a directive.

    Using both directives in XAML for one FrameworkElement or FrameworkContentElement will cause an exception: if the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.

    0 讨论(0)
  • 2020-11-22 05:44

    x:Name and Name are referencing different namespaces.

    x:name is a reference to the x namespace defined by default at the top of the Xaml file.

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

    Just saying Name uses the default below namespace.

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    

    x:Name is saying use the namespace that has the x alias. x is the default and most people leave it but you can change it to whatever you like

    xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml"
    

    so your reference would be foo:name

    Define and Use Namespaces in WPF


    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 x:name and name. All xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" and xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" are is references to multiple namespaces. Since xaml holds the Control namespace(not 100% on that) and presentation holds the FrameworkElement AND the Button class has a inheritance pattern of:

    Button : ButtonBase
    ButtonBase : ContentControl, ICommandSource
    ContentControl : Control, IAddChild
    Control : FrameworkElement
    FrameworkElement : UIElement, IFrameworkInputElement, 
                        IInputElement, ISupportInitialize, IHaveResources
    

    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. So you can say x:Name or Name and they will both be accessing the getter/setter from the FrameworkElement.

    MSDN Reference

    WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The XmlnsDefinitionAttribute 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 http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.

    So the assembly attributes will look something like:

    PresentationFramework.dll - XmlnsDefinitionAttribute:

    [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")]  
    
    0 讨论(0)
  • 2020-11-22 05:45

    The specified x:Name becomes the name of a field that is created in the underlying code when XAML is processed, and that field holds a reference to the object. In Silverlight, using the managed API, the process of creating this field is performed by the MSBuild target steps, which also are responsible for joining the partial classes for a XAML file and its code-behind. This behavior is not necessarily XAML-language specified; it is the particular implementation that Silverlight applies to use x:Name in its programming and application models.

    Read More on MSDN...

    0 讨论(0)
  • 2020-11-22 05:46

    They're both the same thing, a lot of framework elements expose a name property themselves, but for those that don't you can use x:name - I usually just stick with x:name because it works for everything.

    Controls can expose name themselves as a Dependency Property if they want to (because they need to use that Dependency Property internally), or they can choose not to.

    More details in msdn here and here:

    Some WPF framework-level applications might be able to avoid any use of the x:Name attribute, because the Name dependency property as specified within the WPF namespace for several of the important base classes such as FrameworkElement/FrameworkContentElement satisfies this same purpose. There are still some common XAML and framework scenarios where code access to an element with no Name property is necessary, most notably in certain animation and storyboard support classes. For instance, you should specify x:Name on timelines and transforms created in XAML, if you intend to reference them from code.

    If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but an error will result if both are specified on the same element.

    0 讨论(0)
提交回复
热议问题