附加属性有哪些不同的用途?

发布于 2024-09-08 08:36:24 字数 201 浏览 12 评论 0原文

到目前为止,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枉心 2024-09-15 08:36:24

看看附属行为。

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.

Attaching a behavior to an object
simply means making the object do
something that it would not do on its
own

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.

扎心 2024-09-15 08:36:24

当您想要向现有类添加属性时,请使用自定义附加属性:
在 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):

两仪 2024-09-15 08:36:24

正如詹姆斯所说,依恋行为是我见过的最有用的东西。我经常使用的一种方法是将文本放入文本框中,然后在获得焦点时使其消失。

包含示例输入的灰色字符串非常适合此目的。因此,对于“名称”文本框,您可能有字符串“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".

痴意少年 2024-09-15 08:36:24

以下是我发现的一些更高级的用途:

  1. 标记区域(棱镜模式)

http://csharperimage.jeremylikness.com/2010/03/mef-instead-of-prism-for-silverlight-3.html

  1. 提供 XAML 的自定义导出使用托管扩展性框架 (MEF)

http:// /csharperimage.jeremylikness.com/2010/03/custom-export-providers-with-custom.html

(与 #1 相关)

  1. 验证(将验证行为附加到控件)

  2. 本地化(我见过用于访问资源并提供翻译值的附加属性)

  3. 显然行为也是另一个主要因素。

Here are a few more advanced uses I've found for them:

  1. Tagging regions (Prism pattern)

http://csharperimage.jeremylikness.com/2010/03/mef-instead-of-prism-for-silverlight-3.html

  1. Providing custom exports of XAML assets using the Managed Extensibility Framework (MEF)

http://csharperimage.jeremylikness.com/2010/03/custom-export-providers-with-custom.html

(Ties into the bullet for #1)

  1. Validation (attach the validation behavior to the control)

  2. Localization (I've seen attached properties used to access the resources and provide the translated value)

  3. Obviously behaviors are another major one as well.

冰火雁神 2024-09-15 08:36:24

将 ICommand 附加到任何事件:
http ://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx

对于我们这些使用 MVVM 的人来说非常有用。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文