WPF 样式:x:Name 和 x:Type 之间的区别 [英] WPF styles: difference between x:Name and x:Type

查看:31
本文介绍了WPF 样式:x:Name 和 x:Type 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在资源字典中定义样式时,您可以使用任何一种

x:Name="xyz"

x:Type="xyz".

并且可以在 XAML 中引用此样式,例如 {StaticResource xyz}.

大多数示例使用'x:Key',而'name'和'key'的区别在于使用'x:name'可以让您使用这种样式定义代码隐藏?

修正:这个问题是完全错误的.想要询问的是 x:Key 和 x:Name 之间的区别.但是我自己并没有去尝试这段代码,而只是依靠记忆——我以为我在 ResourceDictionary 中都有,这是错误的.而且我在

中也没有这样的代码

 

部分,因为它也不起作用.您不能引用没有 x:Key 的样式(x:Name 在这里不起作用),并且在没有 x:Key 的情况下添加两种样式会引发异常,因为它们在字典中都获得了相同的(空?)键.

雷以一种非常好的方式把所有的差异都放在了一起,谢谢.
我的错

解决方案

这是一个棘手的问题.实际上,您不能在 ResourceDictionary 中使用以下任一方式定义样式

x:Type="xyz"

x:Name="xyz"

区别如下:

  • x:Type="xyz" 不是有效的 XAML 语法.
  • x:Name="xyz" 实际上是有效的 XAML 语法,用于命名对象(这会影响代码隐藏的生成)但不提供字典键.
  • x:Key="xyz" 也是有效的 XAML 语法,它提供字典键但不命名对象.

在字典中需要一个键,因此您必须指定 x:Key(除了 FrameworkTemplate 及其子类,键可以从 TargetType 或 DataType 推断).如果需要,您还可以在字典中指定 x:Name 但它不会影响键.

请注意,x:Type 是 XAML 预定义的标记扩展,而 x:Namex:Key 是实际的 XAML关键词.所以 x:Type 只能在标记扩展语法中用作属性的值:

something="{x:Typewhatever}"

x:Namex:Key 是可用于元素的属性.

While defining style in resource dictionary you can use either

x:Name="xyz"

and

x:Type="xyz". 

and can reference this style in XAML like {StaticResource xyz}.

Most examples use 'x:Key', and the difference between 'name' and 'key' is that using 'x:name' lets you use this style definition code-behind?

FIXES: The question is totally wrong. What was intended to be asked was the difference between x:Key and x:Name. But didn't go trying this code myself, but was just relying on memories - thought I have both in ResourceDictionary, which was wrong. And I also didn't have such code in

 <xxx.Resources > 

sections, since it doesn't work either. You can't reference Style that doesn't have x:Key (x:Name doesn't work here), and adding two styles without x:Key throws exception since both get the same (empty?) key in dictionary.

Ray puts all the difference in a very nice way, thanks.
My fault

解决方案

This is a trick question. In fact, you cannot define a style in a ResourceDictionary using either

x:Type="xyz"

or

x:Name="xyz"

Here is the difference:

  • x:Type="xyz" is not valid XAML syntax.
  • x:Name="xyz" is actually valid XAML syntax that names an object (which affects the generation of code-behind) but does not provide a dictionary key.
  • x:Key="xyz" is also valid XAML syntax that provides a dictionary key but does not name an object.

In a dictionary a key is required, so you must specify x:Key (except that for FrameworkTemplate and its subclasses the key can be inferred from the TargetType or DataType). In a dictionary you may also specify x:Name if desired but it does not affect the key.

Note that x:Type is a markup extension that is predefined by XAML, whereas x:Name and x:Key are actual XAML keywords. So x:Type can only be used in markup extension syntax as the value of a property:

something="{x:Type whatever}"

whereas x:Name and x:Key are attributes that can be used on elements.

这篇关于WPF 样式:x:Name 和 x:Type 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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