Use BindableProperty on a self contained component

202 views Asked by At

I'm trying to set some BindablePropertys on a self contained component, ie a component with it's own view model.

To have access to BindablePropertys, I declare the properties and their accessors:

  public ICommand ItemSelectedCommand
  {
    get => (ICommand)GetValue(ItemSelectedCommandProperty); 
    set { SetValue(ItemSelectedCommandProperty, value); Vm.ItemSelectedCommand = value; }
  }
  
  public ItemSize ItemSize
  {
    get => (ItemSize)GetValue(ItemSizeProperty);
    set { SetValue(ItemSizeProperty, value); Vm.ItemSize = value; }
  }

  public static readonly BindableProperty ItemSelectedCommandProperty = BindableProperty.Create(nameof(ItemSelectedCommand), typeof(ICommand), typeof(Component));
  public static readonly BindableProperty ItemSizeProperty = BindableProperty.Create(nameof(ItemSize), typeof(ItemSize), typeof(Component));

Then I get the view model from the BindingContext

  public SelectorComponentVm Vm => BindingContext as SelectorComponentVm;

So the component's constructor becomes:

  public SelectorComponent()
  {
    InitializeComponent();
    BindingContext = new SelectorComponentVm();
  }

Then I place the component in the MainPage, binding its properties:

  <v:SelectorComponent
      ItemSelectedCommand="{Binding SelectItemCommand}"
      ItemSize="{Binding SelectedSize}"/>

If I set break points in properties setters, I see they are never reached.

Is it a bug in MAUI or a mis-using of BindingProperty?

Test project: MauiTest/TransBinding

0

There are 0 answers