Archive

Archive for the ‘WPF’ Category

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

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

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

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