简体   繁体   中英

Binding a control within a control

I have a page which is databound to an object like so 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;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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