I have a simple app with a slider and a label. when I move the slider I can see the setter(Points) being called and updating the value, but when calling PropertyChanged
the label doesn't update.
What I am expecting it to do is to call the PointsString
getter when PropertyChanged is called. Using the debugger I have confirmed that ProperyChanged
is being called and that the value of points
is being updated. Why is the label bound to PointsString not being updated when the points setter is called?
View.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MyFirstApp.ViewModels"
x:Class="MyFirstApp.Views.DetailPage">
<ContentPage.Content>
<TableView Intent="Form">
<TableRoot>
<TableSection Title="Name">
<EntryCell Label="Name:" />
</TableSection>
<TableSection Title="Rating">
<ViewCell>
<StackLayout Orientation="Horizontal" >
<Label Text="Points:" />
<Label Text="{Binding PointsString}">
<Label.BindingContext>
<vm:DetailsPageViewModel />
</Label.BindingContext>
</Label>
</StackLayout>
</ViewCell>
<ViewCell>
<Grid>
<Slider Maximum="10" Value="{Binding Points , Mode=TwoWay}">
<Slider.BindingContext>
<vm:DetailsPageViewModel/>
</Slider.BindingContext>
</Slider>
</Grid>
</ViewCell>
</TableSection>
</TableRoot>
</TableView>
</ContentPage.Content>
</ContentPage>
ModelView:
using System;
using System.ComponentModel;
class DetailsPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double points;
public DetailsPageViewModel()
{
points = 4.0;
}
public DetailsPageViewModel(double Points) : this()
{
points = Points;
}
public double Points
{
get { return points; }
set
{
Console.WriteLine(value);
points = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("PointsString"));
}
}
}
public string PointsString
{
// Lazy load from points value
get { return points.ToString(); }
}
}
}