ValidationResultChangedEventArgs - in c# and VB are differences !

Oct 23, 2013 at 12:03 PM
Edited Oct 23, 2013 at 12:04 PM
Hello,

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.

In VB.net:
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
        RaisePropertyChanged(propertyName)
    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))
        {
            RaisePropertyChanged(propertyName);
        }
    }
so - in the first look - no difference between these two sequences.
but - i found out that when the ResultChanged Event is fired in VB.net, 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
greenlion
Coordinator
Oct 23, 2013 at 12: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 12: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 vb.net:
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.

Regards
greenlion
Coordinator
Oct 23, 2013 at 12: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 1: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.
https://www.dropbox.com/sh/npsvmn7tjibihjt/u5S25itepR

thanks for looking in to it

regards
greenlion
Coordinator
Oct 23, 2013 at 1: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 2: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 2: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; }
        set 
        {
            _txtTempVon = value;
            RaisePropertyChanged("txtTempVon");
__ 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


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