Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I am new to xamarin.forms

I was trying two way databinding but when it comes to binding with interger property which is nullable it doesnt update viewmodel from view

Canany one please tell me how to handle that

What I have tried:

//View

<StackLayout VerticalOptions="Center">

    <Label Text="Name of the student"></Label>
    <Entry Text="{Binding Person.Name, Mode=TwoWay}"></Entry>

    <Label Text="Age of the person"></Label>
    <Entry Text="{Binding Person.Age, Mode=TwoWay}"></Entry>

    <Label Text="Last Name of the person"></Label>
    <Entry Text="{Binding Person.LastName, Mode=TwoWay}"></Entry>

    <Button Text="SaveTask" Command="{Binding SaveCommand}"></Button>

    <Label Text="{Binding Message}" TextColor="{Binding Message, Converter={StaticResource TextToColorConverter}}"></Label>

  </StackLayout>


//ViewModel

C#
public class HomeViewModel : INotifyPropertyChanged
    {
        public PersonBO _person;
        private string _message;

        public PersonBO Person
        {
            get
            {
                return _person;
            }
            set
            {
                _person = value;
                OnPropertyChanged();
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                OnPropertyChanged();
            }
        }

        public Command SaveCommand
        {
            get
            {
                return new Command(() =>
                {
                    Message = "Your task : " + Person.Name + " ,"
                    + Person.Age + "," + Person.LastName + " was sucessfully saved !";
                });
            }
        }

        public HomeViewModel()
        {
            Person = new PersonBO()
            {
                Name = "Neil",
                Age = 26
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }



    }



//Model

C#
public class PersonBO
    {
        public string Name { get; set; }
        public string LastName { get; set; }
        public int? Age { get; set; }
    }



Here age donest get update when clicking on button
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900