WPF的一个小问题 [英] One small question for WPF

查看:75
本文介绍了WPF的一个小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,WPF专家:

< textbox x:name ="textBox1">一些文本</textbox>
"x:"在这里是什么意思,为什么我们在这里需要它?

谢谢!

Hi, WPF experts:

<textbox x:name="textBox1"> some text</textbox>
what does it mean here "x:", why we need it here?

thanks!

推荐答案

来自文档 a> [ ^ ]

为对象元素处理后存在于运行时代码中的实例指定运行时对象名称
From the documentation[^]

Specifies a run-time object name for the instance that exists in run-time code after an object element is processed


请参见:
http://social.msdn.microsoft.com/论坛/da-DK/wpf/thread/216553bf-9be2-4d0a-acc1-b38af8ef948e [
See this:
http://social.msdn.microsoft.com/Forums/da-DK/wpf/thread/216553bf-9be2-4d0a-acc1-b38af8ef948e[^]

报价:

如果它是"Key"而不是"x:Key",则这将要求"Key"成为 根据XAML甚至XML的语法规则,作为资源添加的元素".

在WPF中,这意味着将每个对象添加为资源
需要公开一个名为"Key"的可写DependencyProperty.

这将排除所有不是DependencyObjects的类型,例如字符串或URI或
ViewModels实例-以及所有不具有名为"Key"的DP的实例,
所以只有所有通常用作资源的对象!

使用"x:Key"指令实现时,键不必是
的真实属性 添加的元素,您可以将其视为元素的附加"属性(寓言地
从技术上讲不是AttachedProperty).
请注意,如果类型具有由DictionaryKeyPropertyAttribute标记的属性,则您
实际上并不需要"x:Key",而是可以使用该类型的"Key"属性(名称可能有所不同),就像您建议的方式一样.

请注意,对于"x:Name"和"Name",该关系与"x:Key"和"Key"的关系大致相同.
如果使用名称",则该元素需要公开一个名为名称"的可写DP,
与键"相反的情况最常见-因为名称"是
的DP 每个FrameworkElement和FrameworkContentElement,因此XAML中使用了超过90%的元素,
所以这就是为什么Name和x:Name经常显示为同义词并且可以互换的原因
(它们并不完整),但是"x:Key"和"Key"却没有.

If it was "Key" rather than "x:Key" this would require "Key" to be an "attribute" of
the "element" added as a resource, according to the syntax rules of XAML and even XML.

In WPF this would mean that every object added as a resource
would need to expose a writable DependencyProperty named "Key".

This would rule out all types that are not DependencyObjects - such as strings or URIs or
ViewModels instances - and more over all those that don''t have such DP named "Key",
so merely all objects commonly used as resources!

With the implementation as "x:Key" directive the key doesn''t need to be a true attribute of
the element added, you can look it as kind of an "attached" attribute to the element (just allegorically
not technically an AttachedProperty).
Note that if a type has a property marked by the DictionaryKeyPropertyAttribute then you
actually don''t need the "x:Key" but may rather use the "Key" property of that type (which may have a different name) - just the way you proposed.

Note that for "x:Name" and "Name" the relation is about the same as for "x:Key" and "Key".
If you use "Name" the element needs to expose a writable DP named "Name",
which in contrast to "Key" is most often the case - because "Name" is a DP of
every FrameworkElement and FrameworkContentElement, so >90% of elements used in XAML,
so that''s why Name and x:Name often appear as synonyms and mutually interchangeable
(which they are not fully) but "x:Key" and "Key" do not.



这同样适用于XAML中的所有x:属性



The same applies to all x: attributtes in XAML


这篇关于WPF的一个小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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