amay077 / Xamarin_Forms_PickerViewSample

PickerView for Xamarin.Forms based on UIPickerView and NumberPicker
http://qiita.com/amay077/items/aaf58d06a067a6ae50db
MIT License
21 stars 14 forks source link

How can I do Itemsource is a List or ObservableRangeCollection, #1

Open aybarsyalcin opened 6 years ago

aybarsyalcin commented 6 years ago

Hi, This project is very well. I have used in many views. But I need the Itemsource is as List then add item programmatically. But I can't do it. Please help me.

I use Modelviewmodel.

List<String> resultList = null;
        public List<String> ResultList
        {
            get { return resultList; }
            set
            {
                resultList = value; OnPropertyChanged();
            }
        }

<controls:PickerView x:Name="xPickerView" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                        ItemsSource="{Binding ResultList}"
                        SelectedIndex="{Binding SelectedIndex}" 
                        BackgroundColor="Transparent"
                        />

when clicked one button. I want to adding items programmatically on pickerview. I can add item to ResultList. But Binding is not firing on Itemsource.

amay077 commented 6 years ago

PickerView.ItemsSource not supports ObservableCollection. Thus, you should set ResultList to new instance of List<string> for notify changes.

Example:

buttonAdd.Clicked += (_, __) => 
{
    var viewModel = this.BindingContext as PickerViewSampleViewModel;

    var oldList = viewModel.ResultList; // ex: [A, B, C]
    var newListInstance = oldList.Concat(new[] { "Z" }).ToList(); // make new instance!
    viewModel.ResultList = newListInstance; 
};

This code is works well.

untitled2