Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bindable Picker's Title not working

I'm working on a Xamarin project and I need to use a picker, so Xamarin's native picker do not have ItemsSource. I've found an implementation that works almost fine, here is:

using System;
using System.Collections;
using System.Linq;
using Xamarin.Forms;

namespace AnyNameSpace.Mobile.CustomControls
{
   public class BindablePicker : Picker
   {
       public static readonly BindableProperty ItemsSourceProperty =
             BindableProperty.Create( 
                                    "ItemSource",
                                    typeof (IEnumerable),
                                    typeof (BindablePicker), 
                                    default(IEnumerable), 
                                    BindingMode.TwoWay,
                                    propertyChanged: OnItemsSourceChanged);

        public static BindableProperty SelectedItemBindableProperty =
              BindableProperty.Create(
                                    "SelectedItemBindable",
                                    typeof (object),
                                    typeof (BindablePicker),
                                    default(object),
                                    BindingMode.TwoWay,
                                    propertyChanged: OnSelectedItemChanged);

        public IEnumerable ItemsSource
        {
            get { return (IEnumerable) GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public object SelectedItemBindable
        {
            get { return GetValue(SelectedItemBindableProperty); }
            set { SetValue(SelectedItemBindableProperty, value); }
        }

        public BindablePicker()
        {
            SelectedIndexChanged += OnSelectedIndexChanged;
            base.Title = "Seleccione"; // Here I want a custom title, but this is ignoring me :(
        }

        private static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
        {
            var picker = bindable as BindablePicker;
            if (picker?.Items == null) return;
            picker.Items.Clear();
            if (newvalue == null) return;
            foreach (var item in ((IEnumerable)newvalue).Cast<object>().Where(item => item != null))
                picker.Items.Add(item.ToString());
        }

        private void OnSelectedIndexChanged(object sender, EventArgs eventArgs)
        {
            if (Items != null && (SelectedIndex < 0 || SelectedIndex > Items.Count - 1))
                SelectedItemBindable = null;
            else if (Items != null) SelectedItemBindable = Items[SelectedIndex];
        }

        private static void OnSelectedItemChanged(BindableObject bindable, object oldvalue, object newvalue)
        {
            var picker = bindable as BindablePicker;
            if (newvalue == null) return;
            if (picker?.Items != null) picker.SelectedIndex = picker.Items.IndexOf(newvalue.ToString());
        }
    }
}

So the problem is: When a set the Title property on XAML or even in C# It always says in Title property "Choose an item" I've tried to set Picker's Title in constructor but do not work.

I'd appreciate any help, thanks.

like image 580
Adrián Romero Avatar asked Mar 01 '26 13:03

Adrián Romero


1 Answers

Setting the Title-Property through Xaml or Code works fine. I just tested this in a small project.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:control="clr-namespace:AnyNameSpace.Mobile.CustomControls" 
         x:Class="TestApp.BindablePickerTestPage">

  <control:BindablePicker Title="MyTitle"></control:BindablePicker>
</ContentPage>
like image 185
Marius Junak Avatar answered Mar 03 '26 03:03

Marius Junak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!