WPF creating custom control - creating control like SpinBox extending TextBox

Posted by veedoo on Stack Overflow See other posts from Stack Overflow or by veedoo
Published on 2010-06-08T12:40:48Z Indexed on 2010/06/08 12:42 UTC
Read the original article Hit count: 672

Filed under:
|

Hi,
What is the best way to extend a control? Lets choose for example SpinBox - we want to extend TextBox to SpinBox - just to add two buttons on the right side, which each one on click checks if Text property is a valid number and if it is increases or decreases the value by 1. Suposse that we have a lot of code using TextBox (Text, IsReadOnly properties and bindings etc.) so we want to replace TextBox by SpinBox without touching rest of the code.
1) we can inherit TextBox - so we have exactly the same interface, but how to add the buttons ??
2) we can inherit from some layout control - eg. DockPanel - we can add buttons, but we lose TextBox interface.
3) after quick read about ControlTemplates I'm not sure, but I believe that we can add buttons and their logic using ControlTemplates
4) like about templates I'm not sure, but I hope we can inherit decorator class to create decorator with the buttons - we lose interface, but can add TextBox as a child
I think that the best solutions is to combine 3) and 4) - create a decorator class and use it in a control template for TextBox.
I would be grateful for your ideas and simple code how to implement 3 and 4, especially with code (c#), not xaml.
Regards

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf-controls