Xamarin custom BindableProperty with Converter

513 Views Asked by At

I have a custom View with a custom BindableProperty:

public string Valore {
            get { return (string)GetValue(ValoreProperty); }
            set { SetValue(ValoreProperty, value); }
        }

    public static readonly BindableProperty ValoreProperty =
        BindableProperty.Create(
            propertyName: nameof(Valore),
            returnType: typeof(string),
            declaringType: typeof(VolosTimePickerView),
            defaultBindingMode: BindingMode.TwoWay,
            propertyChanged: ValorePropertyChanged,
            propertyChanging: ValorePropertyChanging
        );

In my XAML, since the value of Valore is a nullable Int, I must use a converter like this:

<view:VolosTimePickerView Grid.Row="0" Grid.Column="1" 
     Valore="{Binding OrariSelezionati.ViaggioInizioMattina, Converter={StaticResource NullableConverter}}" />

How can I do the same thing, without using Converter={StaticResource NullableConverter in XAML side, but in code behind directly on the property?

Thank you!

1

There are 1 best solutions below

0
On

Your Valore is a string. Try to convert it into Nullable<int> Valore. See what happens.

And try to bind the value of the Valore inside your ViewModel also. So you would get

Nullable<int> _Valore;
public Nullable<int> Valore {get; set;}