Recently I encountered a problem in my custom numeric control, which can take double values. But in certain scenarios it must be restricted to take only integers. Here, I have 2 ways to do it. Either handle it in the textchangedeventhandler of the textbox or create a behavior class for the custom Numeric textbox. I need to understand which one is better as per performance, standards and scalability. I also went through this link How do I get a TextBox to only accept numeric input in WPF?. But need more specific answer as per standards.
-
MVVM? Pure MVVM - behaviors, *dirty* MVVM (or not MVVM) - events. Other option is to make custom control. – Sinatr Dec 01 '14 at 13:35
-
My advice is to handle the textinputevent of your custome numeric control and handle the behavior to have either Duoble or Interger using a Regex – jadavparesh06 Dec 01 '14 at 14:19
1 Answers
My opinion is this. I prefer composition over inheritance. There are many discussions on the web regarding this (irrespective of technology). Perhaps start by reading this: http://en.wikipedia.org/wiki/Composition_over_inheritance
So, when I want to change the behaviour of a WPF control, I will normally use an attached property (or Behavor if you so wish) as my initial hook. In some cases I then have complex modifications where I then have a whole 'listener' class monitoring and reacting to input on the control.
However, there are times when you may need to get at a protected member of a control. If this cannot be avoided then inheritance it is.
So, architecturally I would say prefer an attached property (or Behavor) first, inheritance second.

- 1,999
- 14
- 17
-
Just a footnote to say that attached propety route is not strictly composition. I guess my point is, I just like to avoid inheritance where possible. – James Willock Dec 01 '14 at 22:52