WPF 中 DataTemplate 中的 x:Key、x:Name 和 x:UID 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23935688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:48:24  来源:igfitidea点击:

What is the Difference Between x:Key, x:Name, and x:UID in a DataTemplate in WPF?

wpfxamlkeydifferenceuid

提问by user3685285

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?

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

回答by Gayot Fow

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.

'x:' 指定命名空间,在您的情况下最有可能是“ http://schemas.microsoft.com/winfx/2006/xaml” 您将看到在 Window.Xaml 文件顶部声明的别名。x:Key、x:Name 等都是该命名空间中的指令。

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

相比之下,'Name' 属性(没有 x:)是在 FrameworkElement 类中声明的依赖属性。

x:Key

x:键

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.

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

x:Name

x:姓名

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.

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

x:Uid

x:用户名

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

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

Notes

笔记

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

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

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

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

A complete list of all Xaml directives is given at Xaml Namespace

Xaml 命名空间中给出了所有 Xaml 指令的完整列表

回答by rajibdotnet

If you want to apply the template to all the tabs in your page, you can use x:Type, but if you want to apply it to few tabs and not to all the tabs you can use x:Key.

如果要将模板应用于页面中的所有选项卡,可以使用x:Type,但如果要将其应用于少数选项卡而不是所有选项卡,则可以使用x:Key

Generally you will use x:Keywhen you want to use it as StaticResource in your xaml file. You will provide x:Nameto a control or template when you want to refer it in your code-behind. I have never used X:Uid, but this is what MSDN says,

通常,当您想在 xaml 文件中将其用作 StaticResource 时,您将使用x:Key。当您想在代码隐藏中引用它时,您将为控件或模板提供x:Name。我从未使用过 X:Uid,但 MSDN 是这样说的,

Use x:Uid to identify an object element in your XAML. Typically this object element is an instance of a control class or other element that is displayed in a UI. The relationship between the string you use in x:Uid and the strings you use in a resources file is that the resource file strings are the x:Uid followed by a dot (.) and then by the name of a specific property of the element that's being localized.

使用 x:Uid 来标识 XAML 中的对象元素。通常,此对象元素是显示在 UI 中的控件类或其他元素的实例。您在 x:Uid 中使用的字符串与您在资源文件中使用的字符串之间的关系是,资源文件字符串是 x:Uid 后跟一个点 (.),然后是元素的特定属性的名称正在本地化。