mercredi 7 septembre 2016

WPF Command CanExecute designed Behaviour

My question is generelly said:" Am I using the CanExecute in a wrong design?"

The reason for me asking this question is quite simple, I can't manage to get the flow chart as I need it.

My CanExecute is bound to a HasError Property, which is triggered by Validation of UI Elements via Validation Rules.

The Behaviour I want to achieve is the following:

  1. Command Button is default disabled
  2. Each field has a Validation Rule if needed
  3. The validation Error is only displayed if the user enters wrong context, the initial empty state is not invalid for the user, but invalid for the button.
  4. The button should activate as soon, as every required field, has passed the validation.
  5. The user should not be bothered with validation errors, as long as he has not typed into the field.

So what I tryed to achieve this behavior:

  1. Created Validation Rules
  2. Assigned them to the Bindings.
  3. Set ValidationRule.ValidatesOnTargetUpdated to true
  4. Via a Helper Class I bind the Validation.HasError to a Property on the model.
  5. The CanExecute Evaluator checks the Property HasError and return false or true.

Now my Problem is how to finally implement with this the above Behaviour. Following Problems occur:

  • If I set HasError default to true, it will only correct to false, if the User types in a failure and then correct it. If he types just correct things the HasError will never update. This is because of the Helper Class which only Updates on Source Change. And the Validation.HasError is default false.

  • I cannot validate the fields in the CanExecute, because this would trigger the Error Template and so my Empty fields, which in user perspective are ok, would all trigger and would inform the user, that these fields are not allowed to be empty.

  • A IsDirty Flag wouldn't assist in such case, as I would need to define each Control, that is needed to be validated as required and collect an all over Dirty status of all required fields.

  • If I set HasError default to false, the button is first actiaved, which would need more Checks in the beginning, which is against my behaviour of using CanExecute.

So as you see, with my needs I can't find a way to get a flow chart. So in my mind came the idea, that I'm maybe miss using the CanExecute or Validation in this case.

So I tried to research how Microsoft defines the CanExecute but I wasn't able to get any concrete Information.

So that's why I'm asking you guys for assistence. Any tip is welcome.

tia Synergi

EDIT: Which I forget to mention, Default values in the fields is not possible, as I use a Placeholder Text or Watermark Text to describe what should be put into the fields.

Aucun commentaire:

Enregistrer un commentaire