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

How can I set and get a selected value in my custom picker? The value is not changeable

$
0
0

How can I get the selected item of my custom picker? It always goes back to the initial state
I tried in several ways and I only had to question.

 public class MyDelegate : UIPickerViewDelegate
    {
        List<string> sourceList = new List<string>();
        public MyDelegate(List<string> list)
        {
            sourceList = list;
        }
        public override void Selected(UIPickerView pickerView, nint row, nint component)
        {
            UILabel label = new UILabel();
            label.Text = sourceList[(int)row];
        }

        public override UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
        {

            try
            {

                UIView contentView = new UIView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 80));

                UILabel label = new UILabel();
                label.Frame = contentView.Bounds;
                contentView.AddSubview(label);
                label.Text = sourceList[(int)row];
                label.Lines = 4;
                label.TextAlignment = UITextAlignment.Center;
                //Change the label style
                label.Font = UIFont.SystemFontOfSize(20);
                label.LineBreakMode = UILineBreakMode.TailTruncation;
                return contentView;

            }
            catch (Exception ex)
            {

                throw;
            }

            //return base.GetView(pickerView, row, component, view);
        }
        public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
        {
            return 80;
        }
    }
}


   public class CustomPickerRenderer : PickerRenderer
    {
        List<string> itemList = new List<string>();
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            var element = (CustomPicker)this.Element;
            Picker picker = Element;
            //itemList = picker.Items.ToList();       
                UITextField uITextField = new UITextField();
                uITextField = Control;
                UIPickerView uIPickerView = uITextField.InputView as UIPickerView;       

            if (this.Control != null && this.Element != null && !string.IsNullOrEmpty(element.Image))
            {
                var downarrow = UIImage.FromBundle(element.Image);
                Control.RightViewMode = UITextFieldViewMode.Always;
                this.Control.BorderStyle = UITextBorderStyle.None;
                Control.LeftView = new UIKit.UIView(new CGRect(0, 0, 5, 0));
                Control.LeftViewMode = UIKit.UITextFieldViewMode.Always;
                Control.BorderStyle = UITextBorderStyle.None;
                Control.RightView = new UIImageView(downarrow);
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 89864

Trending Articles



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