Forum Xamarin.Forms

Custom Renderer Nullable DatePicker with EventToCommandBehaviour on Unfocused not working

ledragonledragon Member ✭✭✭

Hi,

I've used the following post to implement a nullable DatePicker:

https://xamgirl.com/clearable-datepicker-in-xamarin-forms/

and the following code to create the NullableDatePicker and bind to a command:

var datePicker = new NullableDatePicker()
{
    BindingContext = rfi,
    FontSize = 14,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    TextColor = Color.White,
    BackgroundColor = Color.FromHex("#4c5a63")
};

datePicker.SetBinding(NullableDatePicker.NullableDateProperty, new Binding("Response", BindingMode.TwoWay, new StringToDateConverter(), "dd/MM/yyyy"));

datePicker.Behaviors.Add(new EventToCommandBehavior()
{
    EventName = "Unfocused",
    Command = _editRfisViewModel.EntryUnfocusedCommand,
    CommandParameter = rfi
});

This implementation doesn't seem to bind the behaviour though as the ViewModel command isn't fired on Unfocus. Can anyone offer some assistance to help how to ensure the Unfocused command fires appropriately?

I've tried;

Element?.Unfocus()

in the custom renderer Dialog Done and Clear event handlers to no avail.

void CreateDatePickerDialog(int year, int month, int day)
{
    NullableDatePicker view = Element;

        _dialog = new DatePickerDialog(Context, (o, e) =>
        {
            view.Date = e.Date;
                ((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                Control.ClearFocus();

                _dialog = null;
        }, year, month, day);

        _dialog.SetButton("Done", (sender, e) =>
        {   
            SetDate(_dialog.DatePicker.DateTime);
                this.Element.Format = this.Element._originalFormat;
                this.Element.AssignValue();
        });

        _dialog.SetButton2("Clear", (sender, e) =>
        {
                this.Element.CleanDate();
                Control.Text = this.Element.Format;
        });
}

Any help much appreciated.

Answers

Sign In or Register to comment.