How to add a placeholder with ability to change placeholder color to DatePicker and TimePicker in Dot Net MAUI?

Ruvindra Senevirathne 1 Reputation point
2024-04-25T14:22:16.44+00:00

Previously in Xamarin.Forms I used below code to do that?

[assembly: ExportRenderer(typeof(CustomDatePicker), typeof(CustomDatePickerRenderer))]

namespace MyEstateLife.Droid.CustomRenderers

{

    public class CustomDatePickerRenderer : DatePickerRenderer

    {

        CustomDatePicker datePicker = null;

        public CustomDatePickerRenderer(Context context) : base(context) 
        {

        }

        void SetColor(Android.Graphics.Color color)`

        {

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)

            {

                Control.BackgroundTintList = ColorStateList.ValueOf(color);

            }

            else

            {

                Control.Background.SetColorFilter(color, PorterDuff.Mode.SrcAtop);

            }

       }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)

        {

            base.OnElementChanged(e);

            if (e.NewElement != null)
            {

                datePicker = Element as CustomDatePicker;

                this.EditText.FocusChange += (sender, ee) => {

                    bool hasFocus = ee.HasFocus;

                    if (hasFocus)

                    {

                        SetColor(datePicker.UnfocusColor.ToAndroid());

                    }

                    else

                   {

                        SetColor(datePicker.BorderColor.ToAndroid());

                    }

                };

                UpdatePickerPlaceholder();

            }

        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)

        {

            base.OnElementPropertyChanged(sender, e);

            if (datePicker != null)

            {

                if (e.PropertyName.Equals(CustomDatePicker.PlaceholderProperty.PropertyName))

                {

                    UpdatePickerPlaceholder();

                }

            }

        }

        void UpdatePickerPlaceholder()

        {

            if (datePicker == null)

                datePicker = Element as CustomDatePicker;

            if (datePicker.Placeholder != null)

            {

                if (!string.IsNullOrWhiteSpace(datePicker.Placeholder))

                {

                    Control.Text = datePicker.Placeholder;

                }

                this.Control.TextChanged += (sender, arg) => {

                    var selectedDate = arg.Text.ToString();

                    if (selectedDate == datePicker.Placeholder)

                    {

                        Control.Text = DateTime.Now.ToString("dd/MM/yyyy");

                    }

                };

                Control.SetHintTextColor(datePicker.PlaceholderColor.ToAndroid());

                SetColor(datePicker.BorderColor.ToAndroid());

            }

        }

    }

}

but how to do this in Dot Net MAUI with handlers?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,959 questions
{count} votes