1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections;
7
using System.Windows.Controls;
9
namespace ICSharpCode.PackageManagement
11
public class SelectedListBoxItemScrollingBehaviour
14
DependencyPropertyChangedEventArgs propertyChangedEventArgs;
16
public SelectedListBoxItemScrollingBehaviour(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
18
this.listBox = dependencyObject as ListBox;
19
this.propertyChangedEventArgs = e;
24
if (ShouldUpdateSelectionChangedHandlerRegistration()) {
25
UpdateSelectionChangedHandlerRegistration();
29
bool ShouldUpdateSelectionChangedHandlerRegistration()
31
return DependencyObjectIsListBox() && NewPropertyValueIsBoolean();
34
bool DependencyObjectIsListBox()
36
return (listBox != null);
39
bool NewPropertyValueIsBoolean()
41
return (propertyChangedEventArgs.NewValue is bool);
44
void UpdateSelectionChangedHandlerRegistration()
46
if ((bool)propertyChangedEventArgs.NewValue) {
47
RegisterSelectionChangedHandler(listBox);
49
UnregisterSelectionChangedHandler(listBox);
53
protected virtual void RegisterSelectionChangedHandler(ListBox listBox)
55
listBox.SelectionChanged += OnListBoxSelectionChanged;
58
protected virtual void UnregisterSelectionChangedHandler(ListBox listBox)
60
listBox.SelectionChanged -= OnListBoxSelectionChanged;
63
static void OnListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
65
OnListBoxSelectionChanged(sender, e.OriginalSource, e.AddedItems, ScrollListBoxItemIntoView);
68
static void ScrollListBoxItemIntoView(ListBox listBox, object item)
70
listBox.ScrollIntoView(item);
73
protected static void OnListBoxSelectionChanged(object sender,
74
object originalSource,
76
Action<ListBox, object> executeScrollListBoxItemIntoView)
78
if (!Object.ReferenceEquals(sender, originalSource)) {
82
ListBox listBox = originalSource as ListBox;
83
if (listBox != null) {
84
if (addedItems.Count > 0) {
85
executeScrollListBoxItemIntoView(listBox, addedItems[0]);