Home>

I want to right justify the characters displayed in Xamarin's Picker. What should I do?
How can I set it up, or can I go without writing a program?
Nice to meet you.

Current status

It looks like this
[abcd]

Purpose

I want to display it like this
[abcd]

  • Answer # 1

    Thecustom rendereris used because it seems unlikely by default.
    Xamarin.Forms custom renderer-Xamarin | Microsoft Docs

    For each platform, you will write the following process. (I named it RightPicker)

    Common

    namespace ○○
    {
        public class RightPicker: Picker
        {
        }
    }

    iOS

    [assembly: ExportRenderer (typeof (RightPicker), typeof (RightPickerRenderer))]
    namespace 〇〇.iOS
    {
        public class RightPickerRenderer: PickerRenderer
        {
            protected override void OnElementChanged (ElementChangedEventArgs<Picker>e)
            {
                base.OnElementChanged (e);
                if (Control! = null)
                {
                    Control.TextAlignment = UIKit.UITextAlignment.Right;
                }
            }
        }
    }

    Android

    [assembly: ExportRenderer (typeof (RightPicker), typeof (RightPickerRenderer))]
    namespace 〇〇.Droid
    {
        public class RightPickerRenderer: PickerRenderer
        {
            public RightPickerRenderer (Context context): base (context)
            {
            }
            protected override void OnElementChanged (ElementChangedEventArgs<Picker>e)
            {
                base.OnElementChanged (e);
                if (Control! = null)
                {
                    Control.Gravity = GravityFlags.Right;
                }
            }
        }
    }
    It is named

    RightPickerand can be used like a normalPicker.

    <local: RightPicker>
        ...
    </local: RightPicker>

Related articles