I’ve adopted what appears to be the standard way of validating textboxes in WPF using the IDataErrorInfo interface and styles as shown below. However, how can I disable the Save button when the page becomes invalid? Is this done somehow through triggers?
Default Public ReadOnly Property Item(ByVal propertyName As String) As String Implements IDataErrorInfo.Item Get Dim valid As Boolean = True If propertyName = 'IncidentCategory' Then valid = True If Len(IncidentCategory) = 0 Then valid = False End If If Not valid Then Return 'Incident category is required' End If End If Return Nothing End Get End Property
<Style TargetType='{x:Type TextBox}'> <Setter Property='Margin' Value='3' /> <Setter Property='Height' Value='23' /> <Setter Property='HorizontalAlignment' Value='Left' /> <Setter Property='Validation.ErrorTemplate'> <Setter.Value> <ControlTemplate> <DockPanel LastChildFill='True'> <Border BorderBrush='Red' BorderThickness='1'> <AdornedElementPlaceholder Name='MyAdorner' /> </Border> </DockPanel> </ControlTemplate> </Setter.Value> </Setter> <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>
A couple of things:
First, I would recommend using the RoutedCommand
ApplicationCommands.Savefor implementing the handling of the save button.If you haven’t checked out the WPF Command model, you can get the scoop here.
Now, to implement the functionality, you can add a command binding to the Window/UserControl or to the Button itself:
Implement these in code behind:
In
Save_CanExecute, sete.CanExecutebased on the validity of the binding on the text box.If you want to implement using the MVVM (Model-View-ViewModel) design pattern, check out Josh Smith’s post on CommandSinkBinding.
One final note: If you want the enable/disable to be updated as soon as the value in the
TextBoxis changed, setUpdateSourceTrigger='PropertyChanged'on the binding for theTextBox.EDIT: If you want to validate/invalidate based on all of the bindings in the control, here are a few suggestions.
1) You are already implementing
IDataErrorInfo. Try implementing theIDataErrorInfo.Errorproperty such that it returns the string that is invalid for all of the properties that you are binding to. This will only work if your whole control is binding to a single data object. Sete.CanExecute = string.IsNullOrEmpty(data.Error);2) Use reflection to get all of the public static DependencyProperties on the relevant controls. Then call
BindingOperations.GetBindingExpression(relevantControl, DependencyProperty)in a loop on each property so you can test the validation.3) In the constructor, manually create a collection of all bound properties on nested controls. In CanExecute, iterate through this collection and validate each
DependencyObject/DepencyPropertycombination by usingBindingOperation.GetBindingExpression()to get expressions and then examiningBindingExpression.HasError.