i've created the following user control for my project and added a bindable property to access the ItemsSource property of the ListView in this user control during XAML in my view, where i use this user control. The code works fine and mainly i know how this works, but not in detail:
namespace ShoppingTracker.UserControls
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ShoppingListScrollView : ContentView
{
public ShoppingListScrollView()
{
InitializeComponent();
}
// Property exposed by the control - to use in XAML file
public ObservableCollection<ShoppingItem> ShoppingItemsDataSource { get; set; }
//public ObservableCollection<ShoppingItem> ShoppingItemsDataSource { get; set; }
// Create bindable property that is tracked by the Xamarin.Forms property system
public static readonly BindableProperty ShoppingItemsDataSourceProperty = BindableProperty.Create(nameof(ShoppingItemsDataSource), typeof(ObservableCollection<ShoppingItem>), typeof(ShoppingListScrollView), null, BindingMode.TwoWay, null, ItemsSourcePropertyChanged);
// Event that should get fired when data in the observable collection changes
private static void ItemsSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (ShoppingListScrollView)bindable;
control.ItemList.ItemsSource = (ObservableCollection<ShoppingItem>)newValue;
}
}
}
What i understand is:
The public property ShoppingItemsDataSource is exposed by the control and can accessed via the XAML file to assign a ObservableCollection of type ShoppingItem.
The ItemsSourcePropertyChanged event is fired and executed, when a value in the ObserverableCollection assigned to ShoppingItemsDataSource changes. So, if a value changes, the ItemsSource of the control is updated with the "changed" ObservableCollection and displays the fresh data.
What i don't understand in detail is, what is happening exactly, when creating the BindableProperty. What does the Create()-method do exactly? And what does these BindableProperty do underneeth the hood exactly? I only know, that this "special" BindableProperty is tracked by the Xamarin.Forms property system, so tracks updates to its source, and that it is neccessary to create, when i want to use data binding, accessing control properties in my user control.
Maybe someone can explain how this and the whole process works in detail or has a good source where i can read or study it myself. I googled a lot and looked for videos, but cant find a satisfying answer for me.
So thats it for my first question here on stackoverflow.
Thanks a lot.