/// <summary> /// Called when SelectedItem had changed /// </summary> /// <param name="parent"><see cref="TabControl"/> where this behavior is attached to</param> /// <param name="e">Change notification</param> private static void OnSelectedItemPropertyChanged(DependencyObject parent, DependencyPropertyChangedEventArgs e) { TabItemGeneratorBehavior instance = GetHandler(parent as TabControl); instance._innerSelection = (null == e.NewValue) ? null : instance._tabControl.Items.Cast <TabItem>().FirstOrDefault(t => e.NewValue.Equals(t.DataContext)); instance.PropertyChanged(instance, new PropertyChangedEventArgs("SelectedTabItem")); }
//------------------------------------------------------------------- // Notification Delegates //------------------------------------------------------------------- #region Dependency Property Notification Delegates /// <summary> /// Called when ItemsSource had changed /// </summary> /// <param name="parent"><see cref="TabControl"/> where this behavior is attached to</param> /// <param name="e">Change notification</param> private static void OnItemsSourcePropertyChanged(DependencyObject parent, DependencyPropertyChangedEventArgs e) { TabItemGeneratorBehavior instance = GetHandler(parent as TabControl); IEnumerable value = e.NewValue as IEnumerable; if (ReferenceEquals(instance._itemsSource, value)) { return; } // Unregister from previous source if (null != instance._itemsSource) { ((INotifyCollectionChanged)instance._itemsSource).CollectionChanged -= instance.OnSourceCollectionChanged; instance._tabControl.Items.Clear(); instance._itemsSource = null; } // Check if source exists if (null == value) { return; } // Register new source INotifyCollectionChanged notifyCollectionChanged = value as INotifyCollectionChanged; if (null == notifyCollectionChanged) { return; } instance._itemsSource = value; notifyCollectionChanged.CollectionChanged += instance.OnSourceCollectionChanged; }