Skip to content

Commit

Permalink
Merge pull request #17847 from ramezgerges/listview_dragdrop_observab…
Browse files Browse the repository at this point in the history
…lecollectionsubclass

fix(listview): dragging when ItemsSource is a subclass of ObservableCollection
  • Loading branch information
ramezgerges authored Aug 9, 2024
2 parents c9cec06 + 54b3ee0 commit 859241f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4679,6 +4679,56 @@ public async Task When_UpdateLayout_In_DragDropping()
}
#endif

#if HAS_UNO
[TestMethod]
[RunsOnUIThread]
#if !HAS_INPUT_INJECTOR
[Ignore("InputInjector is only supported on skia")]
#endif
public async Task When_DragDrop_ItemsSource_Is_Subclass_Of_ObservableCollection()
{
var SUT = new ListView
{
AllowDrop = true,
CanDragItems = true,
CanReorderItems = true,
ItemsSource = new SubclassOfObservableCollection
{
"0",
"1",
"2"
}
};

await UITestHelper.Load(SUT);

var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
using var mouse = injector.GetMouse();

// drag(pick-up) item#0
mouse.MoveTo(SUT.GetAbsoluteBoundsRect().Location + new Point(20, SUT.ActualHeight / 6));
await WindowHelper.WaitForIdle();
mouse.Press();
await WindowHelper.WaitForIdle();

// drop onto item#1
mouse.MoveTo(SUT.GetAbsoluteBoundsRect().Location + new Point(20, SUT.ActualHeight * 2 / 6));
await WindowHelper.WaitForIdle();
mouse.MoveTo(SUT.GetAbsoluteBoundsRect().Location + new Point(20, SUT.ActualHeight * 3 / 6));
await WindowHelper.WaitForIdle();
mouse.Release();
await WindowHelper.WaitForIdle();

var textBlocks = SUT.FindFirstDescendant<ItemsStackPanel>().Children
.Select(c => c.FindFirstDescendant<TextBlock>())
.OrderBy(c => c.GetAbsoluteBoundsRect().Y)
.ToList();
Assert.AreEqual("1", textBlocks[0].Text);
Assert.AreEqual("0", textBlocks[1].Text);
Assert.AreEqual("2", textBlocks[2].Text);
}
#endif

[TestMethod]
[RunsOnUIThread]
public async Task When_ScrollIntoView_FreshlyAddedDefaultItem() // checks against #17695
Expand Down Expand Up @@ -5199,5 +5249,9 @@ public override string ToString()
return Name;
}
}

public class SubclassOfObservableCollection : ObservableCollection<string>
{
}
}
}
15 changes: 13 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/ListViewBase/ListViewBase.DragDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,19 @@ private void CleanupReordering()

#region Helpers
private static bool IsObservableCollection(object src)
=> src.GetType() is { IsGenericType: true } srcType
&& srcType.GetGenericTypeDefinition() == typeof(ObservableCollection<>);
{
var type = src.GetType();

while (type != null && type != typeof(object))
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ObservableCollection<>))
{
return true;
}
type = type.BaseType;
}
return false;
}

private static void DoMove(ItemCollection items, int oldIndex, int newIndex)
{
Expand Down

0 comments on commit 859241f

Please sign in to comment.