ValidationResultChangedEventArgs - in c# and VB are differences !

Oct 23, 2013 at 1:03 PM
Edited Oct 23, 2013 at 1:04 PM

i played around with the MVVMValidation Library in a VB Project - it all worked fine.
Now i tried to get the same Projects to work in c#.

I recognized that there a certain differences between the languages, on the ValidationResultChangedEventArgs Object.

AddHandler Validator.ResultChanged, AddressOf ExtendHookUpValidationNotification
Private Sub ExtendHookUpValidationNotification(ByVal o As Object, ByVal e As Object) Handles _validator.ResultChanged
    Dim propertyName = TryCast(e.Target, String)

    If Not String.IsNullOrEmpty(propertyName) Then
    End If
End Sub
In c#:
Validator.ResultChanged += ExtendHookUpValidationNotification;
private void ExtendHookUpValidationNotification(object sender, ValidationResultChangedEventArgs e)
        dynamic propertyName = Convert.ToString(e.Target);

        if (!string.IsNullOrEmpty(propertyName))
so - in the first look - no difference between these two sequences.
but - i found out that when the ResultChanged Event is fired in, in e.Target is the Propertyname of the Property that has been changed - which is correct.

But in c# in e.Target there is the value of the Property - which is not correct.
How can i get the Propertyname of the validated property, in the resultchanged event?

I'm glad to hear the answer - because i'm having this problem for about 3 days and i couldnt figure out a solution on this.

kind regards
Oct 23, 2013 at 1:14 PM
Could you please show the code where you add the validation rules for both C# and VB? I suspect that the problem might be there.
Oct 23, 2013 at 1:42 PM
thanks for your reply.

here in c#:
        Validator.AddRequiredRule(() => txtDesignDruck, "Eingabe wird benötigt.");
        Validator.AddRule(() => txtDesignDruck,
                            () =>
                                if (txtDesignDruck < 0 || string.IsNullOrEmpty(Convert.ToString(txtDesignDruck)))
                                    return RuleResult.Invalid("Eingabe darf nicht 0 oder Leer sein.");
                                return RuleResult.Valid();
and here a rule definition in
Validator.AddRule(Function() ValditationField, Function() RuleResult.Assert(Not ValditationField = " " And Not String.IsNullOrEmpty(ValditationField), "Die Eingabe darf nicht leer sein"))
i have oriented myself with your documentation on the home tab of this page - and on the demo projects / sources which you have uploaded here.

Oct 23, 2013 at 1:59 PM
Everything looks correct, I don't see the problem. I also cannot reproduce it on my side. Could you possibly create a simple project that reproduces the issue?
Oct 23, 2013 at 2:37 PM
so i tried it out in a new clean project.
but there it works like a charm.

here you can find the 2 projects - one which i tried for testing and one which i'm on it to bringt to work.

thanks for looking in to it

Oct 23, 2013 at 2:54 PM
I've just tried the KSRFloatCalc project and the it works for me. So, if I set the "Designdruck" in UI to "-1" and move focus to another field, I see the field "Designdruck" highlighted with the error message from the validation rule.
However, I noticed that the "Validator.AddRequiredRule" for that field will never work because the field value cannot be NULL or empty string because it is of type "double"! So, if you remove the text from the "Designdruck" text box, you will see an error on UI that the value cannot be converted. That message is coming from the binding system and saying that it cannot convert an empty string to a "double" value.
Oct 23, 2013 at 3:02 PM
hm you're right.
in case you tipe -1 it works.
but try to type 0 - it should validate that too and should throw an error (red border).
because the expression is like: if (txtDesignDruck < 0)
why it does not appear on 0 ?

so, basically it works, right?
Oct 23, 2013 at 3:13 PM
so another conclusion why it doesnt validate is that in the setter of the properties there must be set a delegate on the Validate Metho.
Like so:
    private double _txtTempVon;
    public double txtTempVon
        get { return _txtTempVon; }
            _txtTempVon = value;
__ Validator.Validate(() => txtTempVon);__
Maybe you could mention it on your documentation.

thank you very much for your help !
i guess i can go back programming now :P

Oct 23, 2013 at 3:17 PM
It does not appear on 0 because "if (txtDesignDruck < 0)" is False. Change it to "if (txtDesignDruck <= 0)" and it should work.
Oct 23, 2013 at 3:23 PM
You are welcome and thank you for your feedback! I will try to make the documentation and examples more clear.