Why is my Validatioin Rule not being invoked?

RogerSchlueter-7899 1,326 Reputation points
2022-03-21T19:55:40.543+00:00

Here is the xaml for a textbox on a WPF window:

<TextBox
   x:Name="txtTitle"
   HorizontalAlignment="Stretch"
   Margin="0,5,5,0">
   <TextBox.Text>
      <Binding
         Path="Title"
         UpdateSourceTrigger="LostFocus">
         <Binding.ValidationRules>
            <self:TextRequired />
         </Binding.ValidationRules>
      </Binding>
   </TextBox.Text>
   <TextBox.Style>
      <Style
         TargetType="TextBox">
         <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
               <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)/ErrorContent}"/>
            </Trigger>
         </Style.Triggers>
      </Style>
   </TextBox.Style>
</TextBox>

This TextBox gets the focus when the window is loaded:

txtTitle.Focus()

which is evident by the blinking cursor in the TextBox. Yet the Validation Rule is NOT be invoked when the curson moves to a different control on the window. Putting this in code behind as a check:

Private Sub txtTitle_LostFocus(sender As Object, e As RoutedEventArgs) Handles txtTitle.LostFocus
    Debug.WriteLine("LostFocus")
End Sub

confirms that the LostFocus event is firing. So why is the validation rule not being invoked?

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
818 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.