static void UpdatePosition(ScrollContentPresenter p, bool highlightOnly) { ScrollViewer scrollViewer = UITools.FindAncestor <ScrollViewer>(p); SelectionAdorner _adorner = GetSelectionAdorner(p); if (GetIsDragging(p)) { Point startScrollbarPosition = GetStartScrollbarPosition(p); Point curScrollbarPosition = GetScrollbarPosition(p); Point startPosition = GetStartPosition(p); Point curPosition = Mouse.GetPosition(p); if (!_adorner.IsSelecting) { if (Math.Abs(startPosition.X - curPosition.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(startPosition.Y - curPosition.Y) > SystemParameters.MinimumVerticalDragDistance) { _adorner.IsSelecting = true; Mouse.Capture(p); } } else { Vector offset = Point.Subtract(startScrollbarPosition, curScrollbarPosition); _adorner.StartPosition = Point.Add(startPosition, offset); _adorner.EndPosition = curPosition; UpdateSelection(p, new Rect( new Point(startPosition.X + startScrollbarPosition.X, startPosition.Y + startScrollbarPosition.Y), new Point(curPosition.X + curScrollbarPosition.X, curPosition.Y + curScrollbarPosition.Y)), highlightOnly); } } }
static void EndDragging(ScrollContentPresenter p) { SelectionAdorner _adorner = GetSelectionAdorner(p); if (_adorner.IsSelecting) { UpdatePosition(p, false); _adorner.IsSelecting = false; SetIsDragging(p, false); } }
internal static object PositionCheck(DependencyObject sender, object value) { if (value is Point) { Point ptValue = (Point)value; SelectionAdorner adorner = sender as SelectionAdorner; ptValue.X = Math.Max(ptValue.X, 0); ptValue.X = Math.Min(ptValue.X, adorner.ActualWidth); ptValue.Y = Math.Max(ptValue.Y, 0); ptValue.Y = Math.Min(ptValue.Y, adorner.ActualHeight); return(ptValue); } return(value); }
public static void OnEnableSelectionpChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) { if (s is ListView) { ListView control = s as ListView; var OnScrollChange = (RoutedEventHandler)delegate(object sender, RoutedEventArgs args) { ItemsPresenter ip = UITools.FindVisualChild<ItemsPresenter>(sender as ListView); ScrollContentPresenter p = UITools.FindAncestor<ScrollContentPresenter>(ip); if (GetIsDragging(p) && Mouse.LeftButton == MouseButtonState.Pressed) UpdatePosition(p, true); }; var OnMouseDown = (RoutedEventHandler)delegate(object sender, RoutedEventArgs args) { ClearSelection(sender as ListView); }; var OnSizeChanged = (RoutedEventHandler)delegate(object sender, RoutedEventArgs args) { ItemsPresenter ip = UITools.FindVisualChild<ItemsPresenter>(sender as ListView); ScrollContentPresenter p = UITools.FindAncestor<ScrollContentPresenter>(ip); EndDragging(p); }; Action unloadAdorner = () => { ScrollContentPresenter p = GetLastScrollContentPresenter(control); if (p != null) { SelectionAdorner _adorner = GetSelectionAdorner(p); if (AdornerLayer.GetAdornerLayer(p) != null) AdornerLayer.GetAdornerLayer(p).Remove(_adorner); control.MouseUp -= new MouseButtonEventHandler(OnMouseUp); control.MouseMove -= new MouseEventHandler(OnMouseMove); _adorner.PreviewMouseDown -= new MouseButtonEventHandler(OnPreviewMouseDown); _adorner.MouseMove -= new MouseEventHandler(OnMouseMove); _adorner.MouseUp -= new MouseButtonEventHandler(OnMouseUp); control.RemoveHandler(ListView.SizeChangedEvent, OnSizeChanged); SetSelectionAdorner(p, null); } }; Action attachAdorner = () => { unloadAdorner(); ItemsPresenter ip = UITools.FindVisualChild<ItemsPresenter>(control); ScrollContentPresenter p = UITools.FindAncestor<ScrollContentPresenter>(ip); if (p != null) { SelectionAdorner _adorner = new SelectionAdorner(p); SetSelectionAdorner(p, _adorner); AdornerLayer.GetAdornerLayer(p).Add(_adorner); control.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown); control.MouseUp += new MouseButtonEventHandler(OnMouseUp); control.MouseMove += new MouseEventHandler(OnMouseMove); _adorner.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown); _adorner.MouseMove += new MouseEventHandler(OnMouseMove); _adorner.MouseUp += new MouseButtonEventHandler(OnMouseUp); control.AddHandler(ListView.SizeChangedEvent, OnSizeChanged); SetLastScrollContentPresenter(control, p); } }; if ((bool)e.NewValue == true) { if (control.IsLoaded) attachAdorner(); else control.Loaded += delegate { attachAdorner(); }; control.AddHandler(ScrollViewer.ScrollChangedEvent, OnScrollChange); control.AddHandler(ListView.MouseDownEvent, OnMouseDown); //Monitor view change, and reattach handlers. DependencyPropertyDescriptor viewDescriptor = DependencyPropertyDescriptor.FromProperty(ListView.ViewProperty, typeof(ListView)); viewDescriptor.AddValueChanged (control, delegate { control.Dispatcher.BeginInvoke(DispatcherPriority.Input, attachAdorner); }); } else //If EnableSelection = False { unloadAdorner(); control.RemoveHandler(ScrollViewer.ScrollChangedEvent, OnScrollChange); control.RemoveHandler(ListView.MouseDownEvent, OnMouseDown); SetSelectionAdorner(control, null); } } }
public static void SetSelectionAdorner(DependencyObject target, SelectionAdorner value) { target.SetValue(SelectionAdornerProperty, value); }
public static void OnEnableSelectionpChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) { if (s is ListView) { ListView control = s as ListView; var OnSizeChanged = (RoutedEventHandler) delegate(object sender, RoutedEventArgs args) { ItemsPresenter ip = UITools.FindVisualChild <ItemsPresenter>(sender as ListView); ScrollContentPresenter p = UITools.FindAncestor <ScrollContentPresenter>(ip); EndDragging(p); }; Func <ScrollContentPresenter, AdornerLayer> getAdornerLayer = (p) => { AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(p); if (adornerLayer != null) { return(adornerLayer); } ItemsPresenter ip = UITools.FindVisualChild <ItemsPresenter>(s as ListView); AdornerDecorator ad = UITools.FindAncestor <AdornerDecorator>(ip); try { adornerLayer = AdornerLayer.GetAdornerLayer(ad); if (adornerLayer != null) { return(adornerLayer); } } catch { } return(p.AdornerLayer);; }; Action unloadAdorner = () => { ScrollContentPresenter p = GetLastScrollContentPresenter(control); if (p != null) { SelectionAdorner _adorner = GetSelectionAdorner(p); if (_adorner != null) { getAdornerLayer(p).Remove(_adorner); _adorner.PreviewMouseDown -= new MouseButtonEventHandler(OnPreviewMouseDown); _adorner.MouseMove -= new MouseEventHandler(OnMouseMove); _adorner.MouseUp -= new MouseButtonEventHandler(OnMouseUp); } control.MouseUp -= new MouseButtonEventHandler(OnMouseUp); control.MouseMove -= new MouseEventHandler(OnMouseMove); control.RemoveHandler(ListView.SizeChangedEvent, OnSizeChanged); SetSelectionAdorner(p, null); } }; Action attachAdorner = () => { unloadAdorner(); ItemsPresenter ip = UITools.FindVisualChild <ItemsPresenter>(control); ScrollContentPresenter p = UITools.FindAncestor <ScrollContentPresenter>(ip); if (p != null) { SelectionAdorner _adorner = new SelectionAdorner(p); SetSelectionAdorner(p, _adorner); AdornerLayer adornerLayer = getAdornerLayer(p); if (adornerLayer != null) { adornerLayer.Add(_adorner); _adorner.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown); _adorner.MouseMove += new MouseEventHandler(OnMouseMove); _adorner.MouseUp += new MouseButtonEventHandler(OnMouseUp); } control.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown); control.MouseUp += new MouseButtonEventHandler(OnMouseUp); control.MouseMove += new MouseEventHandler(OnMouseMove); control.AddHandler(ListView.SizeChangedEvent, OnSizeChanged); SetLastScrollContentPresenter(control, p); } }; if ((bool)e.NewValue == true) { if (control.IsLoaded) { attachAdorner(); } else { control.Loaded += delegate { attachAdorner(); } }; control.AddHandler(ScrollViewer.ScrollChangedEvent, new RoutedEventHandler(OnScrollChange)); control.AddHandler(ListView.MouseDownEvent, new RoutedEventHandler(OnMouseDown)); //Monitor view change, and reattach handlers. DependencyPropertyDescriptor viewDescriptor = DependencyPropertyDescriptor.FromProperty(ListView.ViewProperty, typeof(ListView)); viewDescriptor.AddValueChanged (control, delegate { control.Dispatcher.BeginInvoke(DispatcherPriority.Input, attachAdorner); }); } else //If EnableSelection = False { unloadAdorner(); control.RemoveHandler(ScrollViewer.ScrollChangedEvent, new RoutedEventHandler(OnScrollChange)); control.RemoveHandler(ListView.MouseDownEvent, new RoutedEventHandler(OnMouseDown)); SetSelectionAdorner(control, null); } } }