/// <summary> /// Initialize the _rootVisual property (if possible and not already done). /// </summary> private void InitializeRootVisual() { if (null == _rootVisual) { // Try to capture the Application's RootVisual _rootVisual = Application.Current.RootVisual as FrameworkElement; if (null != _rootVisual) { var rootVisual = _rootVisual; // Use a weak event listener. if (_rootVisualMouseMoveListener != null) { _rootVisualMouseMoveListener.Detach(); } _rootVisualMouseMoveListener = new WeakEventListener <ContextMenu, object, MouseEventArgs>(this); _rootVisualMouseMoveListener.OnEventAction = (instance, source, eventArgs) => instance.HandleRootVisualMouseMove(source, eventArgs); _rootVisualMouseMoveListener.OnDetachAction = (weakEventListener) => rootVisual.MouseMove -= weakEventListener.OnEvent; rootVisual.MouseMove += _rootVisualMouseMoveListener.OnEvent; } } }
/// <summary> /// Called when ItemsSource has changed. /// Handles setting the selected item. /// </summary> /// <param name="oldItemsSource">The old items source.</param> /// <param name="itemsSource">The items source.</param> /// <remarks>When switching ItemsSource, the selectedIndex will be used (if possible) /// to select an item in the new collection.</remarks> private void OnItemsSourceChanged(IEnumerable oldItemsSource, IEnumerable itemsSource) { INotifyCollectionChanged oldObservableItemsSource = oldItemsSource as INotifyCollectionChanged; INotifyCollectionChanged newObservableItemsSource = itemsSource as INotifyCollectionChanged; if (oldObservableItemsSource != null) { // no longer react to changes in this collection. // Detach the WeakEventListener if (null != _weakEventListener) { _weakEventListener.Detach(); _weakEventListener = null; } } if (itemsSource != null) { int index = GetIndexOf(itemsSource, Value); if (index > -1) { // no need to select a different value, however, currentIndex needs to be set. CurrentIndex = index; } else { // use the currentindex to set the value // or use the _currentIndexDuringInitialization // or set to first item in list IEnumerable <object> newItemsSource = itemsSource.OfType <object>(); if (_currentIndexDuringInitialization != null && _currentIndexDuringInitialization.Value > -1) { if (IsValidCurrentIndex(CurrentIndex)) { Value = newItemsSource.ElementAt(CurrentIndex); } else { Value = IsValidCurrentIndex(_currentIndexDuringInitialization.Value) ? newItemsSource.ElementAt(_currentIndexDuringInitialization.Value) : newItemsSource.FirstOrDefault(); } _currentIndexDuringInitialization = -1; } else if (newItemsSource.Contains(_valueDuringInitialization)) { // set the value from the initialization cache. Value = _valueDuringInitialization; _valueDuringInitialization = new object(); } else { // get value at the same index of the new source, of get the first. // it is possible for the new source to have 0 items Value = IsValidCurrentIndex(CurrentIndex) ? newItemsSource.ElementAtOrDefault(CurrentIndex) : newItemsSource.FirstOrDefault(); } } if (newObservableItemsSource != null) { // if ItemsSource supplies CollectionChanged events, subscribe to them. // Use a WeakEventListener so that the backwards reference doesn't keep this object alive _weakEventListener = new WeakEventListener <DomainUpDown, object, NotifyCollectionChangedEventArgs>(this); _weakEventListener.OnEventAction = (instance, source, eventArgs) => instance.OnItemsChanged(source, eventArgs); _weakEventListener.OnDetachAction = (weakEventListener) => newObservableItemsSource.CollectionChanged -= weakEventListener.OnEvent; newObservableItemsSource.CollectionChanged += _weakEventListener.OnEvent; } } else { // clear the items collection. _items.Clear(); } // determine if the user can spin. SetValidSpinDirection(); }