BindableApplicationBar.BindableApplicationBar.ButtonsSourceCollectionChanged C# (CSharp) Method

ButtonsSourceCollectionChanged() private method

private ButtonsSourceCollectionChanged ( object sender, NotifyCollectionChangedEventArgs e ) : void
sender object
e System.Collections.Specialized.NotifyCollectionChangedEventArgs
return void
        private void ButtonsSourceCollectionChanged(
            object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (var buttonSource in e.OldItems)
                {
                    // Copy item reference to prevent access to modified closure
                    var dataContext = buttonSource;
                    var button =
                        this.buttonsSourceButtons.FirstOrDefault(
                            b => b.DataContext == dataContext);

                    if (button != null)
                    {
                        this.buttonsSourceButtons.Remove(button);
                    }
                }
            }

            if (this.ButtonsSource != null &&
                this.ButtonTemplate != null &&
                e.NewItems != null)
            {
                foreach (var buttonSource in e.NewItems)
                {
                    var button = (BindableApplicationBarButton)
                        this.ButtonTemplate.LoadContent();

                    if (button == null)
                    {
                        throw new InvalidOperationException(
                            "BindableApplicationBar cannot use the ButtonsSource property without a valid ButtonTemplate");
                    }

                    button.DataContext = buttonSource;
                    this.buttonsSourceButtons.Add(button);
                }
            }
        }
        #endregion