Binding to nullable int

Hi,

I have the need to bind a label text to a nullable int.
I noticed that this somehow isn't working so I made a breakdown version of what i need.
https://github.com/PieterjanDeClippel/XFNullableBindingsIssue

The second entry's value should be available in the viewmodel but somehow the viewmodel variable keeps being empty.
This off course causes the label text to be empty, but the main issue is that the entry text isn't reflected in the viewmodel.

Is this a bug? How do I solve it. I'm not keen on making workarounds for this...

A string is a reference type, so this is already nullable. And this always works fine.
Other basic types are value types, but when not nullable this doesn't seem to be an issue.
When nullable (like double?, int?, ...) this issue occurs.
Currently on XF 3.4.0.1008975

Tagged:

Best Answers

  • PieterjanDeClippelPieterjanDeClippel US ✭✭
    Accepted Answer

    OK. Solved it by binding to string properties and adding conversion in the accessors.

    public string NullableValue
    {
        get
        {
            if (Value == null) return "";
            else return Value.ToString();
        }
        set
        {
            double result;
            var success = double.TryParse(value, out result);
            Value = success ? (double?)result : null;
        }
    }
    

Answers

  • PieterjanDeClippelPieterjanDeClippel USMember ✭✭
    Accepted Answer

    OK. Solved it by binding to string properties and adding conversion in the accessors.

    public string NullableValue
    {
        get
        {
            if (Value == null) return "";
            else return Value.ToString();
        }
        set
        {
            double result;
            var success = double.TryParse(value, out result);
            Value = success ? (double?)result : null;
        }
    }
    
Sign In or Register to comment.