附加属性有哪些不同的用途?
到目前为止,我在 WPF 的冒险经历中已经看到了 AttachedProperties 的一些非常不同的用途,我想知道其中有哪些不同的用途?
我见过相当普通的用途,例如 Grid 和 Canvas 中的用途,以及一些非常酷的技巧,允许在没有 setter 的情况下绑定到集合。您还发现了 AttachedProperties 的哪些其他应用程序? (代码示例非常有帮助!)
I've seen some really varying uses of AttachedProperties so far in my adventures in WPF, and am wondering, what are some of the various uses?
I've seen fairly mundane uses, such as those found in Grid and Canvas, as well as some really cool hacks allowing binding to collections without setters. What other applications have you found for AttachedProperties? (Code samples really helpful!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看看附属行为。
http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx //不确定此链接的质量。我在谷歌搜索后找到的第一个。
可能性是无限的,但它通常可以减少扩展控件或向隐藏代码添加逻辑的需要。
Take a look into attached behaviours.
http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx //not sure on the quality of this link. First one I came to after googling.
The possibilities are pretty limitless on what you can actually do through attached behaviours but it can often reduce the need to extend controls or add logic to a code behind.
当您想要向现有类添加属性时,请使用自定义附加属性:
在 Silverlight 4 中可用之前,我创建了DataGrid 的附加属性,该属性允许列用 * 指定宽度以占用所有可用空间):
Use a custom attached property when you want to add properties to an existing class:
Before it became available in Silverlight 4, I created an attached property for a DataGrid that allowed a column to be specified with a * width to take up all available space):
正如詹姆斯所说,依恋行为是我见过的最有用的东西。我经常使用的一种方法是将文本放入文本框中,然后在获得焦点时使其消失。
包含示例输入的灰色字符串非常适合此目的。因此,对于“名称”文本框,您可能有字符串“Joe Bloggs”。
Attached behaviours are the most useful thing I've seen so far as James said. One I use regularly is for putting text into a textbox and then having it disappear when it gets focus.
A greyed out string containing example input works well for this. So for a 'Name' textbox you might have the string "Joe Bloggs".
以下是我发现的一些更高级的用途:
http://csharperimage.jeremylikness.com/2010/03/mef-instead-of-prism-for-silverlight-3.html
http:// /csharperimage.jeremylikness.com/2010/03/custom-export-providers-with-custom.html
(与 #1 相关)
验证(将验证行为附加到控件)
本地化(我见过用于访问资源并提供翻译值的附加属性)
显然行为也是另一个主要因素。
Here are a few more advanced uses I've found for them:
http://csharperimage.jeremylikness.com/2010/03/mef-instead-of-prism-for-silverlight-3.html
http://csharperimage.jeremylikness.com/2010/03/custom-export-providers-with-custom.html
(Ties into the bullet for #1)
Validation (attach the validation behavior to the control)
Localization (I've seen attached properties used to access the resources and provide the translated value)
Obviously behaviors are another major one as well.
将 ICommand 附加到任何事件:
http ://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx
对于我们这些使用 MVVM 的人来说非常有用。
Attaching an ICommand to ANY event:
http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx
Great for those of us using MVVM.