I have a problem which has taken hours upon hours of my time. I am sure I am missing something obvious. I have reproduced the issue on a simplified form which uses just 1 button and 1 label. Label correctly sets to initial value. Upon button click I am trying to change the label text. From David to Terry.
Command button is firing, setter is getting called, onPropertyChange is getting called. Interestingly after initial debug the get'er is not fired again. (Checked all obvious things, property is public and a property, it is named correctly, specified TwoWay)
...
//--- View Model code ---//
public class TestBindingVM : INotifyPropertyChanged
{
private string profileName;
public ICommand ChangeTextCommand { get; }
public TestBindingVM()
{
ProfileName = "David";
ChangeTextCommand = new Command(UpdateTextCommandAction);
}
public void UpdateTextCommandAction()
{
ProfileName = "Terry";
}
public string ProfileName
{
get => profileName;
set
{
profileName = value;
OnPropertyChanged("ProfileName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var propertyChangedCallback = PropertyChanged;
propertyChangedCallback?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
// ---------------------
// Complete XAML Layout :
//----------------------
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TrackManager.Views.TestBindingPage">
<ContentPage.Content>
<StackLayout>
<Label Text="{Binding Path=ProfileName}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button Margin="0,10,0,0" Text="Change Text"
Command="{Binding ChangeTextCommand}"
TextColor="White"
/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
///----------------------------------------------//
// The page code, creating the bindingcontext //
///----------------------------------------------//
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestBindingPage : ContentPage
{
public ViewModels.TestBindingVM vm = new ViewModels.TestBindingVM();
public TestBindingPage ()
{
this.BindingContext = vm;
InitializeComponent();
}
}
...
This is similar to this post, which did not get a complete answer. Perhaps due to missing code:
Two way binding Not working In Xamarin Forms
It is just so strange that the value "David" is shown on loading. Which shows binding is close to working.
Help appreciated.
Text="{Binding ProfileName}"
– Jason