c# - Restricting Input in WPF -


i creating custom control when invoked in xaml can set allow types of inputs:

<lib:customcontrol restrictto="unsignedintegersonly" ... ></customcontrol>

where unsignedintegersonly part of enum containing set of allowed restrictions.

if user inputs not allowed, control throw validation error , not allow him continue next form/page/etc.

my vision implementing this, to, in underlying textbox makes control, bind text field validation rule passed input restrictto value specified in customcontrol xaml declaration. in validationrule class, handle restrictto specific validation , return whether validation successful or not.

this not quite sure how proceed. possible pass arguments validationrule in such seemingly dynamic manner? setting property, restrictto, of control , passing validation.

if possible, how done? sort of binding or resource linking should use?

you might interested in using maskedtextbox control, restrict user can input in textbox.

as there's no official control microsoft wpf suggest following xceed :

maskedtextbox (it's free use :-)

here have syntax of mask :

http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx

<window x:class="wpfapplication1.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"         title="mainwindow" height="350" width="525">     <grid>         <xctk:maskedtextbox mask="0000"></xctk:maskedtextbox>     </grid> </window> 

if have visual studio 2012 can package through nuget :

(right-click on project )

enter image description here

note : on answer previous question, extensively used validation rules in link posted i'd if there times can avoid through means of well-crafted component/control, it's wise so. @barn pointed out on previous question, generic validation rule might hard thing , questionable you'll have handle types in it, imo it's little counter-intuitive validators , converters specific against being generalist; you're waste more time on it's worth , less re-usable think be. (source : experience)


Comments