I’m creating a WPF window with multiple textboxes, when the user presses the OK button I want all the text boxes to be evaluated for being non-blank.
I understand that I have to use TextBoxes with ‘UpdateSourceTrigger of ‘Explicit’, but do I need to call ‘UpdateSource()’ for each of them ?
e.g.
<TextBox Height="23"
HorizontalAlignment="Left"
Margin="206,108,0,0"
Text="{Binding Path=Definition, UpdateSourceTrigger=Explicit}"
Name="tbDefinitionFolder"
VerticalAlignment="Top"
Width="120" />
<TextBox Height="23"
HorizontalAlignment="Left"
Margin="206,108,0,0"
Text="{Binding Path=Release, UpdateSourceTrigger=Explicit}"
Name="tbReleaseFolder"
VerticalAlignment="Top"
Width="120" />
…
BindingExpression be = tbDefinitionFolder.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
BindingExpression be2 = tbReleaseFolder.GetBindingExpression(TextBox.TextProperty);
be2.UpdateSource();
An alternative approach can be to set your UpdateSourceTrigger to PropertyChanged.
And then inherit your VM from both INotifyPropertyChanged and IDataErrorInfo. Here’s an example…
Assume that one of your TextBoxes is bound to ‘MyProperty’ as declared above. The indexer is implemented in IDataErrorInfo, and gets called when ‘MyProperty’ changes. In the indexer body, you can perform a check if the value is empty and return an error string. If the error string is non-null, the user gets a nice adorner on the TextBox as a visual cue. So you are in one shot performing validation and delivering the UI experience.
All of this is for free if you use the two interfaces as coded above and use UpdateSourceTrigger=PropertyChanged. The use of UpdateSourceTrigger = Explicit is massive overkill for providing the validation you described.
The Xaml for the TextBox would be…