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

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

问题描述

我目前正在将我创建的许多附加行为迁移到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:

我还注意到,行为扩展了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天全站免登陆