WPF中的数据模板中的x:Key,x:Name和x:UID有什么区别? [英] What is the Difference Between x:Key, x:Name, and x:UID in a DataTemplate in WPF?

查看:1397
本文介绍了WPF中的数据模板中的x:Key,x:Name和x:UID有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在WPF中创建动态选项卡,并且试图编写仅适用于某些选项卡项目的内容模板.我希望能够为内容模板创建一个标识符,以便可以在后面的代码中引用它,以便可以有选择地将其应用于单个TabControl中的仅某些选项卡.但是,我对这三个不同的xaml标识符感到困惑.有什么区别,哪一个最适合我的目的?

I am trying to create dynamic tabs in WPF, and I'm trying to write a content template that will only apply to some tab items. I want to be able to create an identifier for the content template so that I can reference it in the code behind, and so that I can selectively apply it to only some tabs in a single TabControl. However, I am confused about these three different xaml identifiers. What is the difference, and which one is best for my purposes?

推荐答案

"x:"指定名称空间,在您的情况下,该名称空间很可能是"

The 'x:' specifies the namespace, which would in your case most likely be "http://schemas.microsoft.com/winfx/2006/xaml" You will see the alias declared at the top of your Window.Xaml file. x:Key, x:Name, etc are all directives in that namespace.

相反,'Name'属性(不带x :)是在FrameworkElement类中声明的依赖项属性.

In contrast, the 'Name' attribute (without the x:) is a dependency property declared in the FrameworkElement class.

x:键

唯一标识在XAML定义的字典中创建和引用的元素.在XAML对象元素中添加x:Key值是在资源字典(例如WPF ResourceDictionary)中标识资源的最常见方法.

Uniquely identifies elements that are created and referenced in a XAML-defined dictionary. Adding an x:Key value to a XAML object element is the most common way to identify a resource in a resource dictionary, for example in a WPF ResourceDictionary.

x:名称

唯一标识XAML名称范围中的XAML定义的元素.当框架提供API或实现在运行时访问XAML创建的对象图的行为时,可以将XAML名称范围及其唯一性模型应用于实例化的对象.

Uniquely identifies XAML-defined elements in a XAML namescope. XAML namescopes and their uniqueness models can be applied to the instantiated objects, when frameworks provide APIs or implement behaviors that access the XAML-created object graph at run time.

x:Uid

为标记元素提供唯一的标识符.在许多情况下,XAML本地化过程和工具都会使用此唯一标识符.

Provides a unique identifier for markup elements. In many scenarios, this unique identifier is used by XAML localization processes and tools.

注释

当应用程序必须通过资源字典支持不同的语言时,我才看到 x:Uid .

I have only seen x:Uid when a app must support different languages with a resource dictionary.

对于其他两个(x:Key和x:Name),基本经验法则是将 x:Name 用于Framework元素,将 x:Key 用于样式,模板等.因此,对于您的问题,如果要命名模板本身,则可以使用 x:Key 指令.在模板中声明的控件将使用 x:Name 指令.

For the other two (x:Key and x:Name), a basic rule of thumb is to use x:Name for Framework elements and x:Key for styles, templates, and so on. So for your question, if you are naming a template itself, you would use the x:Key directive. Controls declared within the template would use the x:Name directive.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆