0
votes

I'm working in a Xamarin Form Project . The form entered values are not showing in the Binding context . Binding Context Always shows the Initial value not the Latest Value .I have added my forms xaml part and View related View model class.Is there any Addition configurations to enable 2 way binding

Xaml Page

 <StackLayout Grid.Row="0" Grid.Column="0">
        <Grid x:Name="grid">
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
         </Grid.ColumnDefinitions>
            <Label x:Name="labelName" Grid.Row="0"  Grid.Column="0" Margin="10" FontSize="6"  VerticalOptions="CenterAndExpand"  HorizontalTextAlignment="Start" Text="Name"/>
                    <Entry  x:Name="textName" Grid.Row="0"  Grid.Column="1"  WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Name,Mode=TwoWay}"  />
            <Label x:Name="labelAge" Grid.Row="1"  Grid.Column="0" Margin="10" FontSize="6"  Text="Age" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <Entry  x:Name="textAge" Grid.Row="1"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Age,Mode=TwoWay}" />
            <Label x:Name="labelAddress" Grid.Row="2"  Grid.Column="0" Margin="10" FontSize="6"  Text="Address" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <Entry  x:Name="textAddress" Grid.Row="2"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start"  Text="{Binding Address,Mode=TwoWay}" />
            <Label x:Name="labelNICNumber" Grid.Row="3"  Grid.Column="0" Margin="10" FontSize="6" Text="NIC" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <Entry  x:Name="textNIC" Grid.Row="3"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding NIC,Mode=TwoWay}" />
            <Button Grid.Row="4"  Grid.Column="1" HeightRequest = "30"   VerticalOptions="CenterAndExpand" HorizontalOptions="Start" FontSize="6" Text="Save" Clicked="UserSaveClick" />
        </Grid>
    </StackLayout>

ViewModel

public event PropertyChangedEventHandler PropertyChanged;

    private string userName ;
    private string name;
    private int age ;
    private bool isBusy;
    private string address;
    private int nic;


    void OnPropertyChanged([CallerMemberName] string name = "")
    {
        PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
    }

    public int NIC
    {
        get { return nic; }
        set
        {
            nic = value;
            OnPropertyChanged();

        }
    }


    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            IsBusy = Name == "aa" ? true : false;
            OnPropertyChanged();
            OnPropertyChanged(nameof(DisplayMessage));
        }
    }



    public int Age
    {
        get { return age; }
        set
        {
            age = value;
            OnPropertyChanged();
        }
    }


    public string Address
    {
        get { return address; }
        set
        {
            address = value;
            OnPropertyChanged();

        }
    }

    public string DisplayMessage
    {
        get
        {
            return "Hi  " + name;
        }

    }




    public bool IsBusy
    {
        get { return isBusy; }
        set { isBusy = value; }
    }


}
2
Have you tried to enter the binding in your codebehind?Carlos Henrique
yes i assign the view model to the binding context and checked the binding context in debug timecharitht99 perera
Does your VM implement INotifyPropertyChanged?Paul Kertscher
Have you checked that the values are updated in the VM?Paul Kertscher
What values do you expect to change?Paul Kertscher

2 Answers

1
votes

Can you check:

1.When entering text in the entry control,check it is hitting the break point in the view model for each property.

2.Check INotifyPropertyChanged is implemented.

3.Check by setting the hard code for the Text property of Entry control.

1
votes

Also check that you properly setter method is not private. I lost some hours to find out this simple copy paste error. I was getting this error in the debug output Binding: 'xxx' property not found on 'yyyy', target property: 'Xamarin.Forms.Entry.Text'. by the way this error message is not super clear.