混合行为 - 你能绑定到它们的属性吗? [英] Blend Behaviours - can you bind to their properties?

查看:14
本文介绍了混合行为 - 你能绑定到它们的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我创建的许多附加行为迁移到 Blend Behaviors,以便它们支持 Expression Blend 中的拖放.我注意到 Blend 行为的作者倾向于将行为属性定义为依赖属性.

I am currently migrating a number of attached behaviours I have created to Blend Behaviours so that they support drag and drop within Expression Blend. I have noticed that authors of Blend behaviours tend to define the behaviour properties as dependency properties.

我创建了一个行为,TiltBehaviour,它公开了一个 double 类型的公共依赖属性,TiltFactor.在 Expression Blend 中,我可以设置此属性的值,但是,添加数据绑定..."的选项是灰色的:

I have created a behaviour, TiltBehaviour, which exposes a public dependency property, TiltFactor, of type double. Within Expression Blend I can set the value of this property, however, the option to add a "Data Binding ..." is grayed out:

我也注意到 Behaviors 扩展了 DependencyObject,因此它们没有 DataContext,因此不能继承元素的 DataContext它们所附.这对我来说就像一个真正的弱点!

I have also noticed that Behaviors extend DependencyObject, therefore they do not have a DataContext and therefore cannot inherit the DataContext of the element to which they are attached. This feels like a real weakness to me!

所以,最重要的是,如果我无法在 Blend 中设置到我的行为依赖属性的绑定,并且它不继承 DataContext,那么为什么还要使用依赖属性呢?我可以只使用 CLR 属性.

So, the bottom-line is, if I cannot set a binding to my behaviors dependency property in Blend, and it does not inherit a DataContext, why bother using dependency properties at all? I could just use CLR properties instead.

推荐答案

dain 是正确的,你仍然可以绑定到人工创建的 DataContext,你多久看到人们绑定到一个SolidColorBrush.Color?即使 SolidColorBrush 继承自 DependencyObject 因而没有 DataContext,它也能工作.

dain is correct you can still bind to the DataContext which is created artificially, how often have you seen people bind to a SolidColorBrush.Color? It also works even though SolidColorBrush inherits from DependencyObject and hence has no DataContext.

参见这篇关于继承上下文的博文.

问题是,由于行为是附加的,它们不会出现在逻辑树中,因此无论如何都不会继承 DataContext.

The thing is that since the behaviours are attached, they do not appear in the logical tree and hence would not inherit a DataContext anyway.

这篇关于混合行为 - 你能绑定到它们的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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