I know that C# isn't C++ in terms that it restricts you from deriving from multiple classes, but what are the ways to overcome it when developing a WPF application.
In my scenario I have a third party control which is a slider, it doesn't derive from content control, hence doesn't have a content property. However I need to have that property. Several solution came to my mind:
- Derive from multiple classes (Not possible in C#)
- Add an attached property to the third party control
- Extend third party control and add a Content property and all other properties which are required for it to function.
- Encapsulate 3rd party control inside my custom control and derive from content control.
What approach开发者_运维知识库 would be the best one? Or if you know any other approaches let me know.
I vote for option 4. Composition seems to be a better option then inheritance in a long run. Later you will be able to replace you third-party control with something else without anybody from outside world knowing about the change. In other words you will encapsulate the dependency on the third-party control in your control.
I would go with option 3. This would allow you to add additional features to the 3rd party control without needing to update it (option 2). This would be useful if the 3rd party provider updates there solution. You wouldnt need to re-add your changes. I wouldnt go with option 4 if you dont need your control to derive from content control. It might be confusing to future developers. So option 3 seems the best.
Option 3... Put a facade of the 3rd-party control on your custom control that indirects to a private instance of the 3rd-party control. Try looking in the disassembly of the 3rd-party control (using Reflector or similar) for interfaces you could implement to assist with the building of the facade.
精彩评论