Author Archive

Custom textbox validation

May 10, 2010 Leave a comment

The task is to use IDataErrorInfo interface to control validation on a textbox(s). We will end up with something that looks like this:


then all you have to do is make the tooltip so that you can see what the error is:

<Style TargetType=”{x:Type TextBox}”>
<Trigger Property=”Validation.HasError” Value=”true”>
<Setter Property=”ToolTip” Value=”{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}”/>
Categories: WPF

Simple textbox validation

May 10, 2010 Leave a comment

The thing that i really get out of this is the UpdateSourceTrigger – i always forget that bit.

For this example, pretend you have a class which holds an integer. Go ahead and bind the textbox to that integer.

How do you validate that an integer is being typed in, as it is being typed?


<TextBox Height="23" Margin="94,0,64,93" Name="TextBox2" VerticalAlignment="Bottom">
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged" >

Categories: Uncategorized

CanExecuteChanged for ICommand

May 3, 2010 Leave a comment

If you want to create a command in WPF, you probably want to tell the application when the command is able to be executed. Of course you put this code in the CanExecute() method. But what about when the conditions change that effect the CanExecute() method?

Add the following code to your Command class, and your button will magically toggle on and off enabled. (or whatever has the command, such as a MenuItem, etc.)

Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
    AddHandler(ByVal value As EventHandler)
        AddHandler CommandManager.RequerySuggested, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler CommandManager.RequerySuggested, value
    End RemoveHandler
    RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End RaiseEvent
End Event

Categories: WPF

Email validation (regex + VB.NET)

May 3, 2010 Leave a comment

Private Shared Function IsValidEmailAddress(ByVal email As String) As Boolean


‘ This regex pattern came from:

Dim pattern As String = “^(?!\.)(“”([^””\r\\]|\\[“”\r\\])*””|([-a-z0-9!#$%&’*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$” 

Return Regex.IsMatch(email, pattern, RegexOptions.IgnoreCase)

 End Function

Categories: Uncategorized