using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
namespace TC_Client.Games.BBWP
{
public class ListScrollBehavior : Behavior<UIElement>
{
/// <summary>
/// 当元素绑定行为以后
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
if (this.AssociatedObject is ListBox)
{
this.AssociatedObject.AddHandler(ListBox.MouseLeftButtonDownEvent, new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown), true);
}
else if (this.AssociatedObject is DataGrid)
{
this.AssociatedObject.AddHandler(DataGrid.MouseLeftButtonDownEvent, new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown), true);
}
else if (this.AssociatedObject is Border)
{
this.AssociatedObject.AddHandler(Border.MouseLeftButtonDownEvent, new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown), true);
}
else
{
this.AssociatedObject.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
}
this.AssociatedObject.MouseMove += new System.Windows.Input.MouseEventHandler(AssociatedObject_MouseMove);
this.AssociatedObject.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);
}
#region 滚动逻辑
/// <summary>
/// 是否按下
/// </summary>
private bool isDragging = false;
/// <summary>
/// 按下时的位置
/// </summary>
private Point mouseOffset;
/// <summary>
/// 滚动条
/// </summary>
private ScrollViewer scroll;
private void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//if (e.MouseDevice.Captured is ButtonBase)
//{
// return;
//}
isDragging = true;
mouseOffset = e.GetPosition(this.AssociatedObject);
if (this.scroll == null)
{
this.scroll = FindVisualChild<ScrollViewer>(this.AssociatedObject);
}
}
private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.isDragging && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
{
Point newPoint = e.GetPosition(this.AssociatedObject);
if (newPoint.Y - mouseOffset.Y > 30)
{
scroll.LineUp();
mouseOffset = newPoint;
}
else if (newPoint.Y - mouseOffset.Y < -30)
{
scroll.LineDown();
mouseOffset = newPoint;
}
}
}
private void AssociatedObject_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (this.isDragging)
{
this.isDragging = false;
}
}
public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
if (obj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
{
return (T)child;
}
T childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
}
return null;
}
#endregion 滚动逻辑
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;
}
}
}