0

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.

  • Exactly? That would require a blog post or tech talk video, showing actual data structures, and the flow of logic. If a google search doesn't find such, then its probably not available. – ToolmakerSteve Jun 11 '23 at 18:29

1 Answers1

0

The scope of your problem is a bit big, beyond the scope of a thread.

But you can first start with official document Bindable properties,which will help you understand and learn Bindable properties.

The purpose of bindable properties is to provide a property system that supports data binding, styles, templates, and values set through parent-child relationships. In addition, bindable properties can provide default values, validation of property values, and callbacks that monitor property changes.

Properties should be implemented as bindable properties to support one or more of the following features:

  • Acting as a valid target property for data binding.
  • Setting the property through a style.
  • Providing a default property value that's different from the default for the type of the property.
  • Validating the value of the property.
  • Monitoring property changes.

Examples of Xamarin.Forms bindable properties include Label.Text, Button.BorderRadius, and StackLayout.Orientation. Each bindable property has a corresponding public static readonly field of type BindableProperty that is exposed on the same class and that is the identifier of the bindable property. For example, the corresponding bindable property identifier for the Label.Text property is Label.TextProperty.

For more information, you can check document Xamarin.Forms Bindable Properties.

And there is a sample included on above link, you can check it.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19