Xamarin.Forms simple Label TextProperty binding

I am new to Xamarin.Forms and the concept of binding. Can someone please tell me why this is not working? The name of the object itself changes when I click the button. Why is not updating the text property?

var red = new Label { Text = todoItem.Name, BackgroundColor = Color.Red, Font = Font.SystemFontOfSize (20) }; red.SetBinding (Label.TextProperty, "Name"); Button button = new Button { Text = String.Format("Tap for name change!") }; button.Clicked += (sender, args) => { _todoItem.Name = "Namie " + new Random().NextDouble(); }; 

TodoItem is an object of the class below. The notification itself works, I'm pretty sure. I assume that something is wrong with my binding, or I missed something with this concept.

 public class TodoItem : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; string _name; public string Name { get { return _name; } set { if (value.Equals(_name, StringComparison.Ordinal)) { // Nothing to do - the value hasn't changed; return; } _name = value; OnPropertyChanged(); } } void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } 
+6
source share
1 answer

You need to bind to the label:

 red.BindingContext = _todoItem; 
+9
source

Source: https://habr.com/ru/post/975426/


All Articles