WPF Validation Result Result

I am trying to do a WPF check in multiple text fields. I want to be able to display validation errors for a user with something more obvious than a tooltip, but more subtle than a dialog box or message box. I decided to use the Popup class instead of some built-in text field to display errors, since there are many different fields that need to be checked in this way, and I want the feedback to be “attached” to the field in question.

The problem I encountered is that the binding of the child TextBox Popup to the attached property TextBox (Validation.Errors) is not updated quite aggressively. As soon as the Error object appears, the text is updated and displayed (ie, “Please enter a name.” For an empty field), but if the error changes (that is, the user enters the wrong text), the message in the pop-up window remains the same. .. until / if they do not enter a valid input, after which the popup disappears as desired.

I did some debugging, and I found that although the validation rule has the correct name and returns the correct results, the converter for the pop-up window is only called when an initial error is generated. I guess it bothers me why Popup is updated only when the verification state goes from "no errors" to "some errors (errors)", so to speak. Does anyone know a way to force changes to Validation.Errors that will appear in my Popup TextBox.Text?

Here is a xaml example of what I wrote.

<TextBox Name="MyTextBox">
    <TextBox.Text>
        <Binding Path="MyText" UpdateSourceTrigger="PropertyChanged" >
            <Binding.ValidationRules>
                <local:MyTextBoxValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
<Popup Name="MyPopup" IsOpen="{Binding ElementName=MyTextBox, 
    Path=(Validation.HasError), Mode=OneWay}">
    <Border BorderThickness="1" BorderBrush="Red" Background="White">
        <TextBlock Foreground="Red" Margin="5 5 5 5" 
            Text="{Binding ElementName=MyTextBox, Path=(Validation.Errors), 
            Converter={StaticResource errorsToMessageConverter}}"/>
    </Border>
</Popup>
+3
source share
2 answers

I could find a compromise. I changed the binding in Popup TextBlock as follows:

<TextBlock Name="MyPopupTextBox" Foreground="Red" Margin="5 5 5 5" 
    Text="{Binding ElementName=MyTextBox, Path=(Validation.Errors)[0].ErrorContent, 
    UpdateSourceTrigger=PropertyChanged, Mode=OneWay, NotifyOnValidationError=True, 
    NotifyOnSourceUpdated=True, ValidatesOnExceptions=True}"/>
+1
source

- : , (insert/remove/clear), . , - , .

, , (ErrorContent) .

(Validation.Errors)[0] , IndexOutOfRange-Exceptions ( , ), .

, , : http://social.msdn.microsoft.com/forums/en/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a/ (Validation.Errors)[0].ErrorContent (Validation.Errors).CurrentItem.ErrorContent, .

0

Source: https://habr.com/ru/post/1783454/


All Articles