简体   繁体   中英

User control accumulates dependency property bindings

I have a user control which is a labelled combo box, through debugging I noticed that each time I open the view which contains the user control there is one more get request to each of the models properties than the previous time!

I do re-instantiate the model each time the view is opened (for reasons that don't really need mentioning), but what appears to be happening is that the binding to any previous copies of the model are retained and the model seems to not be being disposed of correctly because it is still tied into the program through this binding! I appreciate that multiple bindings to the dependency property can be sustained, but how do I stop this happening?

Two considerations I have thought of:

1) The usercontrol is not being disposed of when the view is closed, if this is possible should I turn my attention towards ensuring it is being disposed of? Or is this overkill to the needs of WPF?

2) Instead of re-instantiating the model each time the view is opened should I just implement a copy function in the model base class which transfer all of the properties over opposed to actually creating another instance of the model?

What do you think?

Thanks

Finally fixed this problem, the issue was not limited to my user controls but in fact all controls (including the standard WPF ones) with bindings! When the window was being closed the bindings were not being terminated, this bug/issue/design (maybe it has a use - although I can't see what that would be) seems very strange to me. Initially I did think maybe it was the way I set the bindings up, however with one of the bindings to a normal text box I simply use Text="{Binding name}" - no other configuration defined which could be causing this issue!

Anyway, the solution I found was to add BindingOpertations.ClearAllBindings(this) to the windows closing event. Hope this helps if you are having this problem.

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