I'm building an MVVM Light WPF app using Visual Studio 2015 Update 1. I have the following two search fields: cmbSearchColumn and txtSearchValue. Neither can be blank when the user clicks the Search button. Note that I've got ValidationRules set for both fields.
Here's the relevant XAML:
<TextBlock Grid.Row="1"
Grid.Column="0"
Style="{StaticResource FieldLabel}">
Search Column
</TextBlock>
<StackPanel Grid.Row="1"
Grid.Column="1"
Style="{StaticResource ValidationStackPanel}">
<ComboBox x:Name="cmbSearchColumn"
DisplayMemberPath="MemberName"
IsEditable="True"
ItemsSource="{Binding SearchColumns}"
SelectedValuePath="MemberValue"
Style="{StaticResource ComboBoxStyle}">
<ComboBox.SelectedItem>
<Binding Mode="TwoWay"
Path="SelectedColumn}"
UpdateSourceTrigger="Explicit">
<Binding.ValidationRules>
<helpers:NotEmptyStringValidationRule
Message="Search Column cannot be blank." ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedItem>
</ComboBox>
<TextBlock Style="{StaticResource FieldLabelError}"
Text="{Binding (Validation.Errors)[0].ErrorContent, ElementName=cmbSearchColumn}" />
</StackPanel>
<TextBlock Grid.Row="2"
Grid.Column="0"
Padding="0 0 9 9"
Style="{StaticResource FieldLabel}">
Search Value
</TextBlock>
<StackPanel Grid.Row="1"
Grid.Column="1"
Style="{StaticResource ValidationStackPanel}">
<TextBox x:Name="txtSearchValue" Style="{StaticResource FieldTextBox}">
<TextBox.Text>
<Binding Mode="TwoWay"
Path="SearchValue"
UpdateSourceTrigger="Explicit">
<Binding.ValidationRules>
<helpers:NotEmptyStringValidationRule
Message="Search Value cannot be blank." ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock Style="{StaticResource FieldLabelError}"
Text="{Binding (Validation.Errors)[0].ErrorContent, ElementName=txtSearchValue}" />
</StackPanel>
<Button Grid.Row="4"
Grid.Column="1"
Command="{Binding SearchEmployeesRelayCommand}"
Content="Search"
Style="{StaticResource FieldButton}" />
When the app loads, it immediately displays the error next to the fields, saying that they cannot be blank. However, I need to trigger the validation on them only when the user clicks the Search button.
How do I do this? Thanks.