Manish Tiwari
Manish Tiwari

Reputation: 13

Prism: View is not updated after setting value in ViewModel?

I was unable to bind the values to View from the ViewModel, I can see the values are setting (ServiceList)

Here are the Xaml

XAML:

<Label
                    Grid.Row="0"
                    FontFamily="{StaticResource BoldFont}"
                    FontSize="{StaticResource NormalFontSize}"
                    HorizontalOptions="StartAndExpand"
                    Text="{Binding Heading, Mode=TwoWay}"
                    TextColor="Black"
                    VerticalOptions="CenterAndExpand" />

PageViewModel:

    private string _Heading;
        public string Heading
        {
            get { return _Heading; }
            set { _Heading = value; SetProperty(ref _Heading, value); }
        }

async void LoadData(Service service)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            if (App.Current.Properties.ContainsKey("user_id"))
                data.Add("user_id", App.Current.Properties["user_id"].ToString());
            data.Add("subcategory", service.id.ToString());
            data.Add("city", "ABC");
            UserResponse = await HomeService.GetServices(data);
            if (UserResponse.status == 200)
            {
                Heading = "529 Interior Decorators in Las Vegas";
            }
        }

Upvotes: 1

Views: 102

Answers (1)

Haukinger
Haukinger

Reputation: 10863

This is because SetProperty only raises PropertyChanged if the value actually changes. By setting it beforehand, you prevent that, and the view cannot know that it should update.

You want to write:

public string Heading
{
    get { return _Heading; }
    set { SetProperty(ref _Heading, value); }
}

Note the missing _Heading = value;

Upvotes: 3

Related Questions