~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/PackageManagement/Project/Src/SelectedListBoxItemScrollingBehaviour.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections;
 
6
using System.Windows;
 
7
using System.Windows.Controls;
 
8
 
 
9
namespace ICSharpCode.PackageManagement
 
10
{
 
11
        public class SelectedListBoxItemScrollingBehaviour
 
12
        {
 
13
                ListBox listBox;
 
14
                DependencyPropertyChangedEventArgs propertyChangedEventArgs;
 
15
                
 
16
                public SelectedListBoxItemScrollingBehaviour(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
 
17
                {
 
18
                        this.listBox = dependencyObject as ListBox;
 
19
                        this.propertyChangedEventArgs = e;
 
20
                }
 
21
                
 
22
                public void Update()
 
23
                {
 
24
                        if (ShouldUpdateSelectionChangedHandlerRegistration()) {
 
25
                                UpdateSelectionChangedHandlerRegistration();
 
26
                        }
 
27
                }
 
28
                
 
29
                bool ShouldUpdateSelectionChangedHandlerRegistration()
 
30
                {
 
31
                        return DependencyObjectIsListBox() && NewPropertyValueIsBoolean();
 
32
                }
 
33
                
 
34
                bool DependencyObjectIsListBox()
 
35
                {
 
36
                        return (listBox != null);
 
37
                }
 
38
                
 
39
                bool NewPropertyValueIsBoolean()
 
40
                {
 
41
                        return (propertyChangedEventArgs.NewValue is bool);
 
42
                }
 
43
                
 
44
                void UpdateSelectionChangedHandlerRegistration()
 
45
                {
 
46
                        if ((bool)propertyChangedEventArgs.NewValue) {
 
47
                                RegisterSelectionChangedHandler(listBox);
 
48
                        } else {
 
49
                                UnregisterSelectionChangedHandler(listBox);
 
50
                        }
 
51
                }
 
52
                
 
53
                protected virtual void RegisterSelectionChangedHandler(ListBox listBox)
 
54
                {
 
55
                        listBox.SelectionChanged += OnListBoxSelectionChanged;
 
56
                }
 
57
                
 
58
                protected virtual void UnregisterSelectionChangedHandler(ListBox listBox)
 
59
                {
 
60
                        listBox.SelectionChanged -= OnListBoxSelectionChanged;
 
61
                }
 
62
                
 
63
                static void OnListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
 
64
                {
 
65
                        OnListBoxSelectionChanged(sender, e.OriginalSource, e.AddedItems, ScrollListBoxItemIntoView);
 
66
                }
 
67
                
 
68
                static void ScrollListBoxItemIntoView(ListBox listBox, object item)
 
69
                {
 
70
                        listBox.ScrollIntoView(item);
 
71
                }
 
72
                
 
73
                protected static void OnListBoxSelectionChanged(object sender,
 
74
                        object originalSource,
 
75
                        IList addedItems,
 
76
                        Action<ListBox, object> executeScrollListBoxItemIntoView)
 
77
                {
 
78
                        if (!Object.ReferenceEquals(sender, originalSource)) {
 
79
                                return;
 
80
                        }
 
81
 
 
82
                        ListBox listBox = originalSource as ListBox;
 
83
                        if (listBox != null) {
 
84
                                if (addedItems.Count > 0) {
 
85
                                        executeScrollListBoxItemIntoView(listBox, addedItems[0]);
 
86
                                }
 
87
                        }
 
88
                }
 
89
        }
 
90
}