views:

29

answers:

1

I seem to be having problems with custom controls in WPF. I want to have a simple validating textbox where I can only put doubles. In Forms it was easy, just create a custom control, inherit from TextBox and handle the appropriate events. Is this the way to go in WPF? I can make a custom control, inherit from TextBox, it compiles, but it never shows up in the application. I have read somewhere that inheriting is "out" whatever that means. Am I going about this the wrong way?

A: 

Inheritance is by no means out, it is just less common than it used to be. That's because the look of a control can be changed independently of its behavior. In addition, attached behaviors can be used to supplement a control's behavior in a fashion that can be used across multiple controls.

You might want to check out my answer to this question, where I define an attached behavior to restrict text entry via a regular expression.

HTH, Kent

Kent Boogaart
thanks, that answers my question for now, but this approach will not let me have instance variables which might be useful sometimes. I don't suppose you have an example providing inheritance?
Esben Skov Pedersen
Check out this MSDN documentation: http://msdn.microsoft.com/en-us/library/ms745025.aspx
Kent Boogaart