Custom control bindable Property not getting hit when set from a view

233 Views Asked by At

I've created a control and it has a bindable property, but when I try to set its value, it does not set, when I check its setter, it's not getting hit while debugging, not sure what am I doing wrong.

public decimal MetricValue
        {
            get => (decimal)GetValue(MetricValueProperty);
            set => SetValue(MetricValueProperty, value);
        }

        public static readonly BindableProperty MetricValueProperty =
            BindableProperty.Create(
                propertyName: nameof(MetricValue),
                returnType: typeof(decimal),
                declaringType: typeof(HeightSelection),
                defaultBindingMode: BindingMode.TwoWay,
                propertyChanged: MetricValuePropertyChanged);

I also have a propertychanged, which is not getting raised

 <controls:customControl
                                        CurrentSystemOfMeasure="{Binding CurrentSystemOfMeasure}"
                                        MetricValue="{Binding CurrentHeight}"
                                        TextAlignment="Start"
                                        OnHeightSelectedCommand="{Binding HeightSelectionCommand}"
                                        IsValid="True" />

any inputs would be helpful

2

There are 2 best solutions below

1
stanley Melara On

have you set context in the xaml view?

Here my way:

 public static readonly BindableProperty CancelButtonVisibleAvailableProperty = BindableProperty.Create(
        propertyName: "CancelButtonVisible",
        returnType: typeof(bool),
        declaringType: typeof(SelphiDetailPhotosView),
        defaultValue: true);

    public bool CancelButtonVisible
    {
        get { return (bool)GetValue(CancelButtonVisibleAvailableProperty); }
        set { SetValue(CancelButtonVisibleAvailableProperty, value); }
    }

ContentView needs to have a x:name to set the context in the property IsVisible in your case you need to set the context in Text Property of label to show decimal value

I have set the context making reference to my custom view selphiDetailPhotosView

   <Button IsVisible="{Binding Source={x:Reference selphiDetailPhotosView}, Path=CancelButtonVisible}"
                        Margin="27,0,27,18"
                        BackgroundColor="{StaticResource PlatinumColor}"
                        Command="{Binding CancelCommand}"
                        Text="Cancelar"
                        TextColor="White"
                        TextTransform="None" />



<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="confia.Views.Views.OnBoardingAndLivenessSharedViews.SelphiDetailPhotosView"
   xmlns:svg="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
xmlns:transformation="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
xmlns:views="clr-namespace:confia.Views.Views"
  xmlns:converters="clr-namespace:confia.Views.Converters"
x:Name="selphiDetailPhotosView"
>

Finally call your customcontrol in your ContentPage and bind with your ViewModel

<customControl:SelphiDetailPhotosView CancelButtonVisible="{Binding CanCancel}"/>

1
stanley Melara On

I have replicated your scenario and this work for me.

CodeBehind Custom Control

 public partial class CustomControl : ContentView
{
    public CustomControl()
    {
        InitializeComponent();
    }

    public static readonly BindableProperty MetricValueProperty = BindableProperty.Create(
       propertyName: nameof(MetricValue),
       returnType: typeof(decimal),
       declaringType: typeof(CustomControl),
       defaultBindingMode: BindingMode.TwoWay,
       defaultValue: 0m);

    public decimal MetricValue
    {
        get { return (decimal)this.GetValue(MetricValueProperty); }
        set { this.SetValue(MetricValueProperty, value); }
    }
}

CustomControl Xaml

<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ferreplace.Views.Controls.CustomControl"
x:Name="this"
>
<ContentView.Content>
    <StackLayout HorizontalOptions="CenterAndExpand" >
        <Label Text="Metric Value" FontSize="40"/>
        <Label FontSize="20" Text="{Binding Source={x:Reference  this}, Path=MetricValue}" ></Label>
    </StackLayout>
</ContentView.Content>

ContenPage to call Custom control

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:ferreplace.Views.Controls"
             x:Class="ferreplace.MainPage">
 <StackLayout>
      <controls:CustomControl MetricValue="{Binding MetricValueBinding}"/>
    </StackLayout>
</ContentPage>

ViewModel

 public class MainPageViewModel: BindableBase, IInitializeAsync
{

    private decimal _metricValueBinding;
    public decimal MetricValueBinding
    {
        get
        {
            return _metricValueBinding;
        }
        set
        {
            SetProperty(ref _metricValueBinding, value);
        }
    }

    public MainPageViewModel()
    {
    }

    public async Task InitializeAsync(INavigationParameters parameters)
    {
        MetricValueBinding = 30m;
    }
}

Decimal Binding Result Maybe you forgot default value in bindable property creation