Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to work with nullable properties? #118

Open
ComptonAlvaro opened this issue Apr 24, 2023 · 2 comments
Open

How to work with nullable properties? #118

ComptonAlvaro opened this issue Apr 24, 2023 · 2 comments

Comments

@ComptonAlvaro
Copy link

I have a Textbox that binds to a long? property in the view model.

I would like that if the TextBox is empty, to set the value of the property to null. But when the TextBox is empty, the property in the view model is not changed, it keeps the last digit that the TextBox had.

I would like to have null when the TextBox is empty.

I was trying to intercept the prperty changed event in this way:

    [ObservableProperty]
    long? _myProperty;

    partial void OnMyPropertyChanging(long? value)
    {
        Console.WriteLine($"Name has changed to {value}");
        _myproperty = value;
    }

However the behaviour is not very good. When I write this secuence in the TextBox I get this behaviour:

1.- Write 5. I get 5 as value.
2.- Delete 5, the method is not execute
3.- Write 5, the method is not execute
4.- Write 5, the value of the method is 55

How could I bind to a long? property?

Thanks.

@ellovich
Copy link

ellovich commented Feb 1, 2024

Same problem...

@DaveInCaz
Copy link

Maybe this is what you are looking for: https://stackoverflow.com/a/3302580/3195477

Summary:

Set TargetNullValue to string.empty on the Binding and it will convert the empty string to null,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants