Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

View not binding to ViewModel, but is binding directly to Model

gunsdmgunsdm USMember
edited December 2016 in Xamarin.Forms


I am working on an car monitoring app that retrieves car info from a server and shows it in the app. The ViewModel is supposed to translate the propeties in the model(for example "180" becomes "180 km/miles" or certain states of an enum in the model become true or false). To translate these properties I am calling a method called "UpdateProperties" when a property is the viewmodel has changed:
curModel.PropertyChanged += (sender, args) => this.UpdateProperties(sender, args);

That method calls RaisePropertyChanged for all the properties in the ViewModel. I put some Debug.WriteLine in this method and in RaisePropertyChanged and I can see it is being called. But when I bind something in the View to something in the ViewModel, it is not showing. For example the Username field in my LoginView, if I go to another page where there is also a LoginView, it doesn't have the same username and password in the entry. I have attached 2 screenshots to show what I'm talking about, the entry fields are supposed to be the same.

Another example: This property from the ViewModel is not showing in my View:
<Label Style="{StaticResource DarkTileText}" x:Name="BatteryCapacity" Text="{Binding BatteryCapacityString}" IsVisible="{Binding ShowInfo}" Grid.Row="4" Grid.Column="0" FontSize="22" />
When I bind something in the View directly to the model, it is showing:
<ActivityIndicator Color="#1010bc" IsVisible="{Binding curModel.IsWaiting}" IsRunning="{Binding curModel.IsWaiting}" Grid.Row="1" Grid.Column="0" />
This ActivityIndicator works as expected.

I have attached my ViewModel file, I think that is where it goes wrong, I think something in the UpdateProperties method but I can't figure out what.
The binding to the ViewModel and the UpdateProperties method used to work but now it does not.

I hope I explained it well and I hope somebody can help me with this,
Thanks in advance

Best Answer


  • gunsdmgunsdm USMember

    @AlexDunn said:
    Two things: Your ViewModel is not implementing INotifyPropertyChanged, although you have the PropertyChanged event, it still needs to actually define INotifyPropertyChanged.
    How are you setting the BindingContext of your page? It seems from a high level that you might not be setting the ViewModel to your BindingContext properly like BindingContext = curModel; when you don't set the BindingContext it tries to use the Page's code behind which is likely why you're seeing things show up when setting the binding explicitly to curModel.PropertyName.

    Oh, I don't know when that got deleted.. Well that was it. It works now.

    The BindingContext is set like this in the page:
    BindingContext = App.ViewModel;

    Thank you

Sign In or Register to comment.