Is there any way to disable or "soften" the visual effects of validation errors in the initial state?

What I still have: A WPF application using MVVM, IDataErrorInfo. Everything works as expected.

Each time I open the "Create a new entity" dialog, the user is saluted with a beautiful shape that flashes red all over the place. This is expected from a pure point of view, but it is annoying, and I would like to be able to do one of two things.

  • “Soften” the highlight color with a red error so that you can say orange or yellow to indicate “Required field”. Subsequently, when the user begins to interact with the control switch with red backlight
  • If this is not possible, is there a way to disable error highlighting @ initial state?

I found this post. How can I get WPF NOT to display validation errors when initially displaying the controls? , considering the same problem, but did not answer it.

I considered implementing the "Clear All Error" implementation in my implementation of IDataErrorInfo, as well as the implementation of the InitialState flag, so the errors will not be added until the user changes at least one field, but they have side effects.

  • If I clear all errors after init, the check will be disabled, but the submit button, of course, is turned on :(
  • If I use the InitialState flag, which is also associated with the submit button, to disable it ... I take one more step, but then ALL errors are added with a change in one property field.

So, before I go over and add the InitState flags for the EACH property, although I will stop and ask if there could be a simpler and more general solution for this.

I can send the code if required. However, I implement IDataErrorInfo in the standard way, Dictionary<string,string> for error messages, property fields with OnChange events, and a case switch statement to invoke individual validation methods.

EDIT: That's what I ended up with.

  • I added the flag IsInitState=true for each property of the Required field in VM
  • In the installer code, this flag is false on the first change

      if (Name != value) { Name = value; IsInitState_Name = false; base.OnPropertyChanged("Name"); } 
  • In ValidateName (), a Null / empty check is conditional based on a flag

    if (! IsInitState_Name && String.IsNullOrEmpty (this.Name))

  • The submit button "canExecute" checks if all flags are == false before enabling submit

  • This is additional work, but it is necessary only for the “required / non-zero features” properties. Now the new Create interface is initially without error messages, but the check is fully functional.

+4
source share
3 answers

One option is to not consider the original “empty” state as invalid (thus not causing the user interface to reflect an error), but to perform a separate set of checks (including non-empty ones) before allowing the commit. In this case, you can set an error for each property that was “empty” when the Commit attempt was made. One of the consequences of this is that when creating a field, empty text editing will clear any error in the field, but it may not be too scary.

0
source

You can add a flag to your class, which will be flags if the class should bypass its validation or not.

For example, when a new class is created, the ShouldValidate flag is false by default. If a new class already exists, for example, pulling data from the database, set the ShouldValidate flag to true . When the user runs the Save command, set ShouldValidate to true if it is false.

If your class also has the IsValid property, then set the ShouldValidate parameter to true before running the validation code, because normally, if you call IsValid , you usually expect the validation to be validated (most of my validating classes have this property, and I I usually check IsValid before saving)

+2
source

here is the ControlTemplate, which I use with a less sharp red color, and a tooltip to display verification error messages; sample for ComboBox, but this is the same for TextBox, etc .:

  <ControlTemplate x:Key="ValidationErrorTemplate" TargetType="Control"> <DockPanel> <Border BorderBrush="Red" BorderThickness="1" Opacity="0.3" CornerRadius="3"> <AdornedElementPlaceholder/> </Border> </DockPanel> </ControlTemplate> <Style x:Key="ValidatableComboBoxStyle" TargetType="{x:Type ComboBox}"> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=(AutomationProperties.HelpText)}" /> <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationErrorTemplate}"/> </Style> 

And applies:

  <ComboBox Style="{StaticResource ValidatableComboBoxStyle}" ItemsSource="{Binding Path=...}" SelectedItem="{Binding Path=..., ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.HelpText="Click to select a valid item."/> 

I have no answer to another part of your question, but the above was too long for a comment, so I turned it into aswer anyway.

0
source

All Articles