简体   繁体   English

将控件绑定到控件中

[英]Binding a control within a control

I have a page which is databound to an object like so this.DataContext = anObject; 我有一个页面,它像这样绑定到一个对象this.DataContext = anObject; , the properties of that object binds perfectly. ,该对象的属性将完美绑定。

But now I have a control within that. 但是现在我可以控制了。

<Control2></Control2>

And in the constructor for that control I set this.DataContext = anotherObject; 然后在该控件的构造函数中设置this.DataContext = anotherObject;

The problem I seem to be having when I do this is that this Control2 seems to be double bound when I do {Binding aProperty} it complains about it not being in anotherObject and when I bind {Binding anotherProperty} it'll complain about it not having it in anObject 我好像我这样做时要遇到的问题是,这种控制2似乎是双重绑定,当我不{Binding aProperty}它抱怨它不是anotherObject ,当我绑定{Binding anotherProperty}它会抱怨不能有它在anObject

Any ideas. 有任何想法吗。

I suspect the data context is being set by the page after you've set it in the constructor. 我怀疑在构造函数中设置数据上下文后,该页面正在设置该页面。 Thus you're getting two different binding sources reported at different times. 因此,您将在不同时间报告两个不同的绑定源。

Try listening to the DataContextChanged event in your control. 尝试侦听控件中的DataContextChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM