How to remove maui Date picker underline

Bhuwan 881 Reputation points
2022-11-02T04:12:48.097+00:00

How to remove date picker underline

i am using below code but getting issue

/// <summary>
/// CustomDatePicker
/// </summary>
public class CustomDatePicker : DatePicker
{
}

/// <summary>
/// CustomDatePickerRenderer
/// </summary>
class CustomDatePickerRenderer : DatePickerRenderer
{
/// <summary>
/// CustomDatePickerRenderer
/// </summary>
/// <param name="context"></param>
public CustomDatePickerRenderer(Context context) : base(context)
{
}

    /// <summary>  
    /// OnElementChanged  
    /// </summary>  
    /// <param name="e"></param>  
    protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)  
    {  
        base.OnElementChanged(e);  
        if (Control != null)  
        {  
            GradientDrawable gd = new GradientDrawable();  
            gd.SetColor(global::Android.Graphics.Color.Transparent);  
            this.Control.SetBackgroundDrawable(gd);  
        }  
    }  
}  

MAUI program

handlers.AddHandler(typeof(CustomDatePicker), typeof(Platforms.Android.Controls.CustomDatePickerRenderer));

256264-image.png

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} vote

Accepted answer
  1. Anonymous
    2022-11-02T05:54:18.373+00:00

    Hello,

    You can use handler to remove maui Date picker underline like following code. I make a test in MainPage, So I post the DatePickerHandler code in the MainPage's constructor for testing.

    Please notice, handler.PlatformView.SetBackgroundDrawable(gd); is deprecated, I use handler.PlatformView.SetBackground(gd); , it is working.

       public MainPage()  
           {  
               InitializeComponent();  
               Microsoft.Maui.Handlers.DatePickerHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>  
               {  
         
                  if (view is CustomDatePicker)  
                   {  
       #if ANDROID  
                       GradientDrawable gd = new GradientDrawable();  
                       gd.SetColor(global::Android.Graphics.Color.Transparent);  
                       // handler.PlatformView.SetBackgroundDrawable(gd); deprecated  
                       handler.PlatformView.SetBackground(gd);  
       #endif  
                   }  
               });  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Miguel Pires 5 Reputation points
    2024-03-14T15:50:46.55+00:00

    Do you need to do anything beyond that? I did the same and it didn't work here. Sorry for my ignorance, I'm a beginner.

    MauiProgram.cs

    User's image

    My App

    User's image

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.