Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Adding a placeholder and aligning text to center on DatePicker

$
0
0

I'm having trouble with adding a placeholder to my DatePicker (doesn't have title property like a regular picker). I have a custom renderer:

public class DOBPicker : DatePicker { public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "Placeholder", returnType: typeof(string), declaringType: typeof(DOBPicker), defaultValue: default(string)); public string Placeholder { get; set; } }

iOS Renderer:

`protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (this.Control == null)
return;
var element = e.NewElement as DOBPicker;
if (!string.IsNullOrWhiteSpace(element.Placeholder))
{
Control.Text = element.Placeholder;
}

       Control.ShouldEndEditing += (textField) => {
           var seletedDate = (UITextField)textField;
           var text = seletedDate.Text;
           if (text == element.Placeholder)
           {
               Control.Text = DateTime.Now.ToString("dd/MMMM/yyyy");
           }
           return true;
       };
    }
    private void OnCanceled(object sender, EventArgs e)
    {
        Control.ResignFirstResponder();
    }
    private void OnDone(object sender, EventArgs e)
    {
        Control.ResignFirstResponder();
    }`

It works, but when I go back to the previous page, the app crashes and I get an error (Object reference not set to an instance of an object). I think it's referring to

if (!string.IsNullOrWhiteSpace(element.Placeholder)) { Control.Text = element.Placeholder; }


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>