我试图找出这些方法之间的一些差异。是否存在使用行为并且无法使用附加属性完成相同功能的情况?
4 回答
No. Behaviors are basically just a much nicer abstraction on top of attached properties.
By using Behavior<T>
, you gain access to the AssociatedObject
directly, as well as the ability to attach and detach the behavior, easily, at runtime.
You could do this with attached properties, but it would require adding a lot of extra plumbing.
我倾向于使用行为来添加可以进行可见更改的功能。而我使用附加属性将附加信息添加到随后由其他对象使用的对象。
例如 Grid.Row 是一个很好的附加属性,因为它由 Grid 而不是目标使用。另一方面,自动更正会产生良好的行为,因为这会在对象上产生可见的变化。
Behaviors are based on attached properties. That means if you can't find specific behavior - you write your own, either based on behaviors framework provided by Blend or by creating your own AP...
I always thought that behaviors are great evidence of attached properties power. Just incredible what you can get with them.
这不是答案。但我能在这个主题上找到最好的解释 https://web.archive.org/web/20180208143035/http://briannoyesblog.azurewebsites.net/2012/12/20/attached-behaviors-vs-attached-properties- vs-混合行为/
Brian Noyes 在他的文章中清楚地描述了每个概念之间的差异。