IEnumerable BindableProperty for CustomRender

Steven Alexander Checo Terrero 1 Reputation point
2021-05-16T09:53:44.933+00:00

Hi guys, I'm trying to do a custom component for picker inside a Frame to make a rounded style, like the image below

96907-image.png

Everything with the style works perfectly, the problem is when I try to create a BindableProperty to fill the Picker's ItemSource property, I tried many things from the internet, but the class never get the data, simple properties as (string, int, bool) work perfectly too, the problems is just with collections (IEnumerable, IList).. this is my code

<?xml version="1.0" encoding="utf-8" ?>  
<Frame xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"   
       xmlns:Controls="clr-namespace:MPDEdesur.CustomControls"  
       x:Class="MPDEdesur.CustomUIControls.PickerRounded"  
       CornerRadius="10"  
       BorderColor="#d8dae6"  
       HasShadow="False"  
       Padding="10"  
       BackgroundColor="#f5f6f8">  
  
    <Controls:CustomDialogPicker   
        x:Name="CustomStylePicker"/>  
</Frame>  


namespace MPDEdesur.CustomUIControls  
{  
    [XamlCompilation(XamlCompilationOptions.Compile)]  
    public partial class PickerRounded : Frame  
    {  
        public PickerRounded()  
        {  
            InitializeComponent();  
        }  
  
  
        public static BindableProperty PlaceHolderProperty = BindableProperty.Create(  
            nameof(PlaceHolder),   
            typeof(string),   
            typeof(PickerRounded), "",   
            BindingMode.TwoWay,   
            propertyChanged: SetPlaceHoldereField);  
  
  
        public string PlaceHolder { get => (string)GetValue(PlaceHolderProperty); set => SetValue(PlaceHolderProperty, value); }  
  
        private static void SetPlaceHoldereField(BindableObject bindable, object oldValue, object newValue)  
        {  
            var view = (PickerRounded)bindable;  
            if (view != null)  
            {  
                view.CustomStylePicker.PlaceHolder = newValue.ToString();  
            }  
        }  
  
        public static BindableProperty ItemsSourceProperty = BindableProperty.Create(  
            nameof(ItemsSource),   
            typeof(IList),   
            typeof(PickerRounded),   
            default(IList),   
            BindingMode.TwoWay,   
            propertyChanged: SetItemCollectionProperty);  
          
          
        public IList ItemsSource { get => (IList)GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); }  
  
        private static void SetItemCollectionProperty(BindableObject bindable, object oldValue, object newValue)  
        {  
            var picker = bindable as PickerRounded;  
  
            if (picker != null)  
            {  
                picker.CustomStylePicker.Items.Clear();  
  
                picker.CustomStylePicker.ItemsSource = (IList)newValue;  
            }  
        }  
    }  
}  

This is how I'm usign the custom control

<ControlsUI:PickerRounded x:Name="Estado" ItemsSource="{Binding EstadoList}" PlaceHolder="Testest"  HorizontalOptions="FillAndExpand"  
                                              Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />  

and this is how I'm populating the object

public ObservableCollection<string> EstadoList = new ObservableCollection<string>();  
  
public MainPageViewModel(INavigationService navigationService) : base(navigationService)  
        {  
            RegisterCommand();  
            LoadData.Execute(null);  
        }  
  
private void RegisterCommand()   
        {  
            LoadData = new Command(() =>  
            {  
                EstadoList.Clear();  
                EstadoList.Add("AND-00");  
                EstadoList.Add("AND-15-FINCA DESHABITADA");  
                EstadoList.Add("AND-16-FINCA EN CONSTRUCCION");  
                EstadoList.Add("AND-21-CLIENTE SIN FACTURA");  
                EstadoList.Add("AND-22-CLIENTE SIN CONTRATO");  
                EstadoList.Add("AND-31-LAMPARA ENCENDIDA");  
            });  
        }  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
{count} votes

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.