WPF: Own Control Derived From TextBox

  • Thread starter Thread starter hufaunder
  • Start date Start date
H

hufaunder

I would like to create a specialized TextBox (WPF) that for instance
only accept integers/decimals/currency/etc. Since I want it to behave
like a TextBox but don't want to write one myself I would like to
create a user control. The idea was to derive a user control (Add
Items...UserControl) from a TextBox but Visual Studio does not let me
define a base class. I could go and change it by hand but I guess
there is a reason why MS didn't offer it in the first place.

So what is the best approach to write this custom TextBox?

Thanks
 
I would like to create a specialized TextBox (WPF) that for instance
only accept integers/decimals/currency/etc. Since I want it to behave
like a TextBox but don't want to write one myself I would like to
create a user control. The idea was to derive a user control (Add
Items...UserControl) from a TextBox but Visual Studio does not let me
define a base class. I could go and change it by hand but I guess
there is a reason why MS didn't offer it in the first place.

So what is the best approach to write this custom TextBox?

What you need is a "CustomControl", the difference between "UserControl" and
"CustomControl" is that, in VS, userControl are created with a XAML file and
are not expected to be templatable, whereas CustomControl have no XAML
associated with them and are expected to be lookless.
 
What you need is a "CustomControl", the difference between "UserControl" and
"CustomControl" is that, in VS, userControl are created with a XAML file and
are not expected to be templatable, whereas CustomControl have no XAML
associated with them and are expected to be lookless.




- Show quoted text -

Thanks for the reply. I did this. Then I changed the base class from
Control to TextBox. Then in my main xaml file I added

xmlns:MyNamespace="clr-namespace:Test"
....
<MyNamespace:NumericTextBox Name="myname"....></
MyNamespace:NumericTextBox>

Now the compiler complains:
1) In the XAML file: NumericTextBox cannot be a element in Grid
2) In the cs file: The name 'myname' does not exist in the current
context

I did recompile the whole project without success. Any input?
 
Thanks for the reply. I did this. Then I changed the base class from
Control to TextBox. Then in my main xaml file I added

xmlns:MyNamespace="clr-namespace:Test"
...
<MyNamespace:NumericTextBox Name="myname"....></
MyNamespace:NumericTextBox>

Now the compiler complains:
1) In the XAML file: NumericTextBox cannot be a element in Grid
2) In the cs file: The name 'myname' does not exist in the current
context

I did recompile the whole project without success. Any input?
Is NumericTextBox in the same project/assembly or in an other one?
if it's in an other one you should use instead

xmlns:MyNamespace="clr-namespace:Test;assembly=TheDLLName"

(notice the additional assembly information)
have you added the DLL in the list of DLL?


whant are the compile error messages? it's really hepful to look at them!
 
Is NumericTextBox in the same project/assembly or in an other one?
if it's in an other one you should use instead

xmlns:MyNamespace="clr-namespace:Test;assembly=TheDLLName"

(notice the additional assembly information)
have you added the DLL in the list of DLL?

whant are the compile error messages? it's really hepful to look at them!

Yes, NumericTextBox is in the same project. I tried this here anyways:

xmlns:MyNamespace="clr-namespace:Test;assembly=Test"

Note, both were copied directly from what VS generated at the top in
the comment section. This is the error I am getting:

error MC3074: The tag 'NumericTextBox' does not exist in XML namespace
'clr-namespace:Test'. Line 32 Position 8.

The class looks something like this:

namespace Test
{
public class NumericTextBox : System.Windows.Controls.TextBox
{
static NumericTextBox() {...}
}
....
}
 
Back
Top