问题
i'm beginner in Wpf programmation, i need to know the difference between Name
and x:Name
<label Name="lab" ></label>
and
<label x:Name="lab" ></label>
what is the differences between the two lines above?. In code behind, How can i differentiate between Name
and x:Name
?
回答1:
Please See this:
They refer to the same thing, but you have to use the x: prefix when defining a UserControl
.
In WPF, what are the differences between the x:Name and Name attributes?
Is there any difference in x:name and name for controls in xaml file?
This from Microsoft:
any WPF application scenarios can avoid any use of the x:Name attribute, because the Name dependency property as specified in the default XAML namespace for several of the important base classes such as FrameworkElement and FrameworkContentElement satisfies this same purpose. There are still some common XAML and WPF scenarios where code access to an element with no Name property at the framework level is important. For example, certain animation and storyboard support classes do not support a Name property, but they often need to be referenced in code in order to control the animation. You should specify x:Name as an attribute on timelines and transforms that are created in XAML, if you intend to reference them from code later. If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but a parse exception will result if both are specified on the same element. If the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load. Name can be set using XAML attribute syntax, and in code using SetValue; note however that setting the Name property in code does not create the representative field reference within the XAML namescope in most circumstances where the XAML is already loaded. Instead of attempting to set Name in code, use NameScope methods from code, against the appropriate namescope. Name can also be set using property element syntax with inner text, but that is uncommon. In contrast, x:Name cannot be set in XAML property element syntax, or in code using SetValue; it can only be set using attribute syntax on objects because it is a directive.
来源:https://stackoverflow.com/questions/20798175/differentiate-between-xname-and-name-in-wpf-application