Blog.Mobile.Components.CustomPicker.OnItemsSourcePropertyChanged C# (CSharp) Method

OnItemsSourcePropertyChanged() private static method

Called when [items source property changed].
private static OnItemsSourcePropertyChanged ( BindableObject bindable, IEnumerable value, IEnumerable newValue ) : void
bindable BindableObject The bindable.
value IEnumerable The value.
newValue IEnumerable The new value.
return void
        private static void OnItemsSourcePropertyChanged(BindableObject bindable, IEnumerable value, IEnumerable newValue)
        {
			var picker = (CustomPicker)bindable;
            var notifyCollection = newValue as INotifyCollectionChanged;
            if (notifyCollection != null)
            {
                notifyCollection.CollectionChanged += (sender, args) =>
                {
                    if (args.NewItems != null)
                    {
                        foreach (var newItem in args.NewItems)
                        {
                            picker.Items.Add((newItem ?? "").ToString());
                        }
                    }
                    if (args.OldItems != null)
                    {
                        foreach (var oldItem in args.OldItems)
                        {
                            picker.Items.Remove((oldItem ?? "").ToString());
                        }
                    }
                };
            }

            if (newValue == null)
                return;

            picker.Items.Clear();

            foreach (var item in newValue)
                picker.Items.Add((item ?? "").ToString());
        }