c# - 区分x :Name and Name in Wpf application

标签 c# .net wpf xaml tags

<分区>

我是 Wpf 编程的初学者,我需要知道 Namex:Name 之间的区别

<label Name="lab" ></label>

<label x:Name="lab" ></label>

上面两行有什么区别?在代码隐藏中,我如何区分 Namex:Name

最佳答案

请参阅this :

它们指的是同一事物,但是在定义 UserControl 时必须使用 x: 前缀。

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?

这来自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.

关于c# - 区分x :Name and Name in Wpf application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20798175/

相关文章:

c# - 可观察集合中的事务支持

c# - 为什么 C# 编译器会产生方法调用以在 IL 中调用 BaseClass 方法

c# - 如果按钮在 1 秒内按下 X 次,执行此操作?

c# - linq - 为绘图填写稀疏表

c# - 是否可以禁用功能区控件上的应用程序菜单? (WPF)

c# - Bing map 在使用自定义图钉时需要大量性能

.net - 如何使用SmtpClient.SendAsync发送带有附件的电子邮件?

c# - EntityFramework.dll 中出现类型为 'System.TypeInitializationException' 的未处理异常

c# - WPF 设计器无法找到 SQLite.Interop.dll

c# - 不同 DataGrid 行的不同选择颜色