Patrick Rainer

Patrick Rainer

  • NA
  • 35
  • 2.6k

Xamarin, Bindable Property

Oct 23 2020 6:07 AM
Dear all
 
It looks like I do misunderstand something in bindable properties. I would appreciate if somebody could explain me, how to do it correctly and why it works like that.
 
Why does the Binding on the "MeetingControl for "Meeting.Subject" not work?
 
GitHub Link: https://github.com/PatrickRainer/BindablePropertyExample
 
This is my Main Page:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage x:Name="MyPage"  
  3.  xmlns="http://xamarin.com/schemas/2014/forms"  
  4.  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  5.  xmlns:local="clr-namespace:SimpleSfTimePicker"  
  6.  x:Class="SimpleSfTimePicker.MainPage"  
  7.  BindingContext="{x:Reference Name=MyPage}">  
  8.     <StackLayout BackgroundColor="LightCyan">  
  9.             <Entry Text="{Binding Meeting.Subject}"/>  
  10.             <local:MeetingControl x:Name="MeetingControl"   
  11.  Subject="{Binding Meeting.Subject}"/>  
  12.         </StackLayout>  
  13. </ContentPage>  
This is the Code Behind (For ease I used no Vm):
  1. public partial class MainPage {  
  2.   Meeting _meeting;  
  3.   
  4.   public MainPage() {  
  5.     InitializeComponent();  
  6.   
  7.     Meeting = new Meeting {  
  8.       Subject = "A Meeting",  
  9.     };  
  10.   }  
  11.   
  12.   public Meeting Meeting {  
  13.     get =>_meeting;  
  14.     set {  
  15.       _meeting = value;  
  16.       OnPropertyChanged();  
  17.     }  
  18.   }  
  19. }  
The Meeting:
  1. public sealed class Meeting: INotifyPropertyChanged {  
  2.   string _subject;  
  3.   
  4.   public string Subject {  
  5.     get =>_subject;  
  6.     set {  
  7.       if (value == _subject) return;  
  8.       _subject = value;  
  9.       OnPropertyChanged();  
  10.     }  
  11.   }  
  12.   
  13.   public event PropertyChangedEventHandler PropertyChanged;  
  14.   
  15.   [NotifyPropertyChangedInvocator]  
  16.   void OnPropertyChanged([CallerMemberName] string propertyName = null) {  
  17.     PropertyChanged ? .Invoke(thisnew PropertyChangedEventArgs(propertyName));  
  18.   }  
  19. }  
The Meeting Control Xaml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentView x:Name="MyContentView"  
  3. xmlns="http://xamarin.com/schemas/2014/forms"  
  4. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  5. xmlns:til="clr-namespace:Syncfusion.XForms.TextInputLayout;assembly=Syncfusion.Core.XForms"  
  6. x:Class="SimpleSfTimePicker.MeetingControl"  
  7. BindingContext="{x:Reference MyContentView}">  
  8. <StackLayout>  
  9. <til:SfTextInputLayout  
  10. Hint="Subject">  
  11. <Entry x:Name="SubjectEntry"/>  
  12. </til:SfTextInputLayout>  
  13. </StackLayout>  
  14. </ContentView>  
The MeetingControl Code Behind:
  1. [XamlCompilation(XamlCompilationOptions.Compile)]  
  2. public partial class MeetingControl {  
  3.   public static readonly BindableProperty SubjectProperty = BindableProperty.Create(nameof(Subject), typeof(string), typeof(MeetingControl), "default", BindingMode.TwoWay, null, PropertyChanged);  
  4.   
  5.   static void PropertyChanged(BindableObject bindable, object oldvalue, object newvalue) {  
  6.     var str = (string) newvalue;  
  7.     var control = (MeetingControl) bindable;  
  8.     control.SubjectEntry.Text = str;  
  9.   }  
  10.   
  11.   public string Subject {  
  12.     get {  
  13.       return (string) GetValue(SubjectProperty);  
  14.     }  
  15.     set {  
  16.       SetValue(SubjectProperty, value);  
  17.     }  
  18.   }  
  19.   
  20.   public MeetingControl() {  
  21.     InitializeComponent();  
  22.   }  
  23. }  

Answers (2)