Skip to content

Commit 762d8ad

Browse files
authored
Merge pull request antonpup#2183 from gitmacer/fix/ReordableListBox
Fix Scroll support for ReordableListBox
2 parents 641f7f9 + 7708f69 commit 762d8ad

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Project-Aurora/Project-Aurora/Controls/ReorderableListBox.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Aurora.Utils;
2+
using System;
23
using System.Collections;
34
using System.Collections.Generic;
45
using System.Collections.ObjectModel;
@@ -28,6 +29,11 @@ public class ReorderableListBox : ListBox {
2829
/// Used for drop index detection. Will not work properly if items are different heights.</summary>
2930
private double itemHeight = 0;
3031

32+
private ScrollViewer _scrollViewer;
33+
public ScrollViewer ScrollViewer => _scrollViewer ??= this.FindChildOfType<ScrollViewer>();
34+
35+
private Point ApplyScrollOffset(Point p) => new Point(p.X + ScrollViewer.HorizontalOffset, p.Y + ScrollViewer.VerticalOffset);
36+
3137
/// <summary>Get the ListBox style and create a new ReorderableListBox style based on it.</summary>
3238
/// <remarks>For whatever reason the style isn't inherited from the ListBox, even though we are extending it.</remarks>
3339
private static Style listBoxStyle = new Style(typeof(ReorderableListBox), Application.Current.TryFindResource(typeof(ListBox)) as Style);
@@ -36,7 +42,6 @@ public class ReorderableListBox : ListBox {
3642
/// this as a base for the items here.</summary>
3743
private static Style listboxItemStyle = Application.Current.TryFindResource(typeof(ListBoxItem)) as Style;
3844

39-
4045
public ReorderableListBox() : base() {
4146
// We want to use the custom ReorderableListBoxPanel with out ReorderableListBoxes
4247
ItemsPanel = new ItemsPanelTemplate(new FrameworkElementFactory(typeof(ReorderableListBoxPanel)));
@@ -82,7 +87,7 @@ private void ReorderableListBoxItem_PreviewMouseLeftButtonDown(object sender, Mo
8287
// Also check that if the developer has specified a "DragElementTag", the original source of the event (the actual clicked element, even if it is
8388
// inside the element with the attached listener, so in this case a particular object in the item template) has a tag that matches the provided one.
8489
if (sender is ListBoxItem item && (DragElementTag == null || DragElementTag.Equals((e.OriginalSource as FrameworkElement)?.Tag))) {
85-
startDragPoint = e.GetPosition(this);
90+
startDragPoint = ApplyScrollOffset(e.GetPosition(this));
8691
draggedItem = item;
8792
}
8893
}
@@ -94,7 +99,7 @@ private void ReorderableListBoxItem_PreviewMouseLeftButtonDown(object sender, Mo
9499
/// </summary>
95100
private void ReorderableListBox_MouseMove(object sender, MouseEventArgs e) {
96101
if (draggedItem != null) {
97-
var delta = e.GetPosition(this) - startDragPoint; // Calculate distacne between current mouse point and original mouse down point
102+
var delta = ApplyScrollOffset(e.GetPosition(this)) - startDragPoint; // Calculate distacne between current mouse point and original mouse down point
98103
if (Math.Abs(delta.X) >= SystemParameters.MinimumHorizontalDragDistance || Math.Abs(delta.Y) >= SystemParameters.MinimumVerticalDragDistance) // Check it's moved a certain distance
99104
BeginDragDrop();
100105
}
@@ -125,7 +130,7 @@ private void ReorderableListBox_DragOver(object sender, DragEventArgs e) {
125130
// Guard to ensure that the drag originated from this list box (if from elsewhere, draggedItem will be null)
126131
if (draggedItem == null) return;
127132

128-
var y = e.GetPosition(this).Y; // Y position of the mouse relative to `this` listbox
133+
var y = ApplyScrollOffset(e.GetPosition(this)).Y; // Y position of the mouse relative to `this` listbox
129134
panel.DropIndex = CalculateDropIndex(y); // Calculate the index that the item would be inserted if the user dropped here (and pass to panel for rendering)
130135
panel.DraggedY = y; // Also pass the relative Y coordinate to the panel, also for rendering
131136
}
@@ -139,7 +144,7 @@ private void ReorderableListBox_Drop(object sender, DragEventArgs e) {
139144

140145
var itemData = draggedItem.DataContext; // The actual data for the item that the user dragged is contained within the DataContext of draggedItem
141146
var oldIndex = Items.IndexOf(draggedItem.DataContext); // Get the old index of the item, based on the current index of the dragged item
142-
var newIndex = CalculateDropIndex(e.GetPosition(this).Y); // Get the new index of item, based on the user's mouse's Y location
147+
var newIndex = CalculateDropIndex(ApplyScrollOffset(e.GetPosition(this)).Y); // Get the new index of item, based on the user's mouse's Y location
143148

144149
// If the new index is after the old index, we must subtract one from it since it will change when we remove the item from the old index location.
145150
if (oldIndex < newIndex) newIndex--;

0 commit comments

Comments
 (0)