| |
|
Add a Masked Textbox control to a WPF program
|
| |
|
|
| |
| I found some very creative and technically challenging examples for adding a
masked textbox control to a WPF program. Most of them required the creation of a new class
inherited from the base System.Windows.Controls.TextBox class. This is a valid option, however,
I have implemented a different approach.
|
| |
| I discovered the System.Windows.Forms.Integration.WindowsFormsHost class which is
specifically created to support the implementation of Windows Forms controls into a WPF program.
Having many years of experience with Windows Forms programs, this was a good find, because there
are a number of really good controls written for Windows Forms that can be used in a WPF
program via the WindowsFormsHost class. One in particular is a Masked TextBox.
|
| |
|
To achieve this add the System.Windows.Forms namespace to the references of you project. Then add
the below line of code to the XAML file within the Window element tag.
|
| |
xmlns:wpfForm=
"clr-namespace:System.Windows.Forms;
assembly=System.Windows.Forms"
|
| |
|
Open the XAML file you want to add the Masked TextBox to and from the Toolbox tab, drag and drop the
WindowsFormsHost onto the WPF window as shown below.
|
| |
 |
| |
|
Modify the XAML so it resembles the below.
|
<WindowsFormsHost Height="24"
HorizontalAlignment="Left"
Margin="23,16,0,0"
Name="windowsFormsHost1"
VerticalAlignment="Top"
Width="70">
<wpfForm:MaskedTextBox x:Name="maskedTextBoxDate"
Mask="00/00/0000" />
</WindowsFormsHost>
|
| |
|
When the program is run, the result is a WPF window with a masked textbox.
|
| |
 |
| |
|
NOTE: You will need to add the logic to confirm the entered data is in fact a valid date,
valid currency value or a valid whatever you choose to mask. This is not done automatically.
|
| |
| It is possible to use generic WPF logic to create and implement a masked textbox,
this is just a simpler way of doing it. |
| |
| Download the source |
| |
|
|
| |
|
|
| |
|
|
| |
| |
| Posts: 113 |
| Comments:
86 |
| Fundamentals:
16 |
| |
 |
| |
| |
|
| |
 |
| |
 |
| |
 |
| |
|
| 2011 December (2) |
| 2011 November (6) |
| 2011 October (7) |
| 2011 September (7) |
| 2011 August (9) |
| 2011 July (9) |
| 2011 June (8) |
| 2011 May (9) |
| 2011 April (7) |
| 2011 March (9) |
| 2011 February (8) |
| 2011 January (8) |
| 2010 December (7) |
| 2010 November (8) |
| 2010 October (4) |
| |
| |
|
| |
|
|
| |
| |
|
The sample code on this website is provided to illustrate a concept and should not be used in
applications or Web sites without proper professional consultation, as it may not illustrate
the safest coding practices. I assume no liability for incidental or consequential damages
should the sample code be used for purposes other than as intended.
|
| |
|
| | | |