-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Animation; | ||
using System.Windows.Shapes; | ||
using Microsoft.Phone.Controls; | ||
using System.Windows.Controls.Primitives; | ||
using System.Collections; | ||
|
||
namespace sbbs_client_wp7 | ||
{ | ||
public class ExtendedListBox : ListBox | ||
{ | ||
protected bool _isBouncy = false; | ||
private bool alreadyHookedScrollEvents = false; | ||
|
||
public ExtendedListBox() | ||
{ | ||
this.Loaded += new RoutedEventHandler(ListBox_Loaded); | ||
} | ||
|
||
private void ListBox_Loaded(object sender, RoutedEventArgs e) | ||
{ | ||
ScrollBar sb = null; | ||
ScrollViewer sv = null; | ||
if (alreadyHookedScrollEvents) | ||
return; | ||
|
||
alreadyHookedScrollEvents = true; | ||
this.AddHandler(ExtendedListBox.ManipulationCompletedEvent, (EventHandler<ManipulationCompletedEventArgs>)LB_ManipulationCompleted, true); | ||
sb = (ScrollBar)FindElementRecursive(this, typeof(ScrollBar)); | ||
sv = (ScrollViewer)FindElementRecursive(this, typeof(ScrollViewer)); | ||
|
||
if (sv != null) | ||
{ | ||
// Visual States are always on the first child of the control template | ||
FrameworkElement element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement; | ||
if (element != null) | ||
{ | ||
VisualStateGroup vgroup = FindVisualState(element, "VerticalCompression"); | ||
VisualStateGroup hgroup = FindVisualState(element, "HorizontalCompression"); | ||
if (vgroup != null) | ||
vgroup.CurrentStateChanging += new EventHandler<VisualStateChangedEventArgs>(vgroup_CurrentStateChanging); | ||
if (hgroup != null) | ||
hgroup.CurrentStateChanging += new EventHandler<VisualStateChangedEventArgs>(hgroup_CurrentStateChanging); | ||
} | ||
} | ||
|
||
} | ||
|
||
public delegate void OnCompression(object sender, CompressionEventArgs e); | ||
public event OnCompression Compression; | ||
|
||
private void hgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e) | ||
{ | ||
if (e.NewState.Name == "CompressionLeft") | ||
{ | ||
_isBouncy = true; | ||
if (Compression != null) | ||
Compression(this, new CompressionEventArgs(CompressionType.Left)); | ||
} | ||
|
||
if (e.NewState.Name == "CompressionRight") | ||
{ | ||
_isBouncy = true; | ||
if (Compression != null) | ||
Compression(this, new CompressionEventArgs(CompressionType.Right)); | ||
} | ||
if (e.NewState.Name == "NoHorizontalCompression") | ||
{ | ||
_isBouncy = false; | ||
} | ||
} | ||
|
||
private void vgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e) | ||
{ | ||
if (e.NewState.Name == "CompressionTop") | ||
{ | ||
_isBouncy = true; | ||
if (Compression != null) | ||
Compression(this, new CompressionEventArgs(CompressionType.Top)); | ||
} | ||
if (e.NewState.Name == "CompressionBottom") | ||
{ | ||
_isBouncy = true; | ||
if (Compression != null) | ||
Compression(this, new CompressionEventArgs(CompressionType.Bottom)); | ||
} | ||
if (e.NewState.Name == "NoVerticalCompression") | ||
_isBouncy = false; | ||
} | ||
|
||
private void LB_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e) | ||
{ | ||
if (_isBouncy) | ||
_isBouncy = false; | ||
} | ||
|
||
private UIElement FindElementRecursive(FrameworkElement parent, Type targetType) | ||
{ | ||
int childCount = VisualTreeHelper.GetChildrenCount(parent); | ||
UIElement returnElement = null; | ||
if (childCount > 0) | ||
{ | ||
for (int i = 0; i < childCount; i++) | ||
{ | ||
Object element = VisualTreeHelper.GetChild(parent, i); | ||
if (element.GetType() == targetType) | ||
{ | ||
return element as UIElement; | ||
} | ||
else | ||
{ | ||
returnElement = FindElementRecursive(VisualTreeHelper.GetChild(parent, i) as FrameworkElement, targetType); | ||
} | ||
} | ||
} | ||
return returnElement; | ||
} | ||
|
||
private VisualStateGroup FindVisualState(FrameworkElement element, string name) | ||
{ | ||
if (element == null) | ||
return null; | ||
|
||
IList groups = VisualStateManager.GetVisualStateGroups(element); | ||
foreach (VisualStateGroup group in groups) | ||
if (group.Name == name) | ||
return group; | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public class CompressionEventArgs : EventArgs | ||
{ | ||
public CompressionType Type { get; protected set; } | ||
|
||
public CompressionEventArgs(CompressionType type) | ||
{ | ||
Type = type; | ||
} | ||
} | ||
|
||
public enum CompressionType { Top, Bottom, Left, Right }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters