Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Children property is not being updated after drag complete #1002

Closed
dpaulino opened this issue Jul 5, 2019 · 8 comments
Closed

Children property is not being updated after drag complete #1002

dpaulino opened this issue Jul 5, 2019 · 8 comments
Assignees
Labels
area-TreeView bug Something isn't working team-Controls Issue for the Controls team

Comments

@dpaulino
Copy link
Contributor

dpaulino commented Jul 5, 2019

Describe the bug
The Children property of an item is not being updated after a different item is dragged into it.

Steps to reproduce the bug

  1. Clone the master branch of this repository: https://github.com/dpaulino/treeviewtest
  2. Press F5 to launch the uwp app
  3. Drag the item named second into the item named first. This will trigger logic that effectively "cancels" the drop and reinserts second back into the root list.
  4. Now do the opposite. Drag first into second.
  5. Crash occurs because the Children property of second was not updated.

Expected behavior
No crash

Version Info

NuGet package version:
Microsoft.UI.Xaml 2.2.190702001-prerelease

Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2019 Update (18362) Yes
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop yes
Mobile
Xbox
Surface Hub
IoT

Additional context
This may be related to my previous issue #631

@jevansaks
Copy link
Member

@kaiguo sounds similar to the other bug you fixed. It looks like this data source is observable so I think it's doing the right thing on the app side for this to work.

@jevansaks jevansaks added the bug Something isn't working label Jul 9, 2019
@dpaulino
Copy link
Contributor Author

dpaulino commented Aug 6, 2019

Hey all, any updates on this issue? Let me know if I can help determine root cause. My customers have been asking for drag and drop in my app for 3 months now and this issue is blocking me.

@kaiguo
Copy link
Contributor

kaiguo commented Aug 6, 2019

Hey all, any updates on this issue? Let me know if I can help determine root cause. My customers have been asking for drag and drop in my app for 3 months now and this issue is blocking me.

Looking at it now...

@kaiguo
Copy link
Contributor

kaiguo commented Aug 6, 2019

@dpaulino Some operations are async due to the way ItemSource and Layout update works, so the children collection in ItemsSource doesn't have anything yet when you try to access it.

It should work if you put your code in a dispatcher like this,

private void TreeView_DragItemsCompleted(MUXC.TreeView sender, MUXC.TreeViewDragItemsCompletedEventArgs args)
{
    if (args.Items[0] is ExplorerItem item)
    {
        var dispatcher = CoreApplication.MainView.Dispatcher;
        var ignore = dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => {
                ExplorerItem parent = GetParent(ViewModel.Nodes, item);
                parent.Children.Remove(item);
                ViewModel.Nodes.Add(item);
            });
    }
    originalParent = null;
}

I think you can probably consider overriding OnDrop method on TreeViewItem to prevent the operation happening from the beginning instead of manually "cancel" the drag&drop by swapping items back.

@dpaulino
Copy link
Contributor Author

dpaulino commented Aug 8, 2019

I will try the OnDrop method, and I'll report back soon.

@dpaulino
Copy link
Contributor Author

@kaiguo can you point out exactly which "OnDrop" method I'm supposed to use? I tried the code below but the event is never fired when I drop a tree view item onto another tree view item.

<c:TreeViewItem ItemsSource="{x:Bind Children}" Content="{x:Bind Name}" AllowDrop="True"
                            Drop="TreeViewItem_Drop" DropCompleted="TreeViewItem_DropCompleted"/>
        private void TreeViewItem_Drop(object sender, DragEventArgs e)
        {
            // breakpoint here is never hit
        }

        private void TreeViewItem_DropCompleted(UIElement sender, DropCompletedEventArgs args)
        {
            // breakpoint here is never hit
        }

Here's a branch you can clone to test out the above code: https://github.com/dpaulino/treeviewtest/tree/dropOnTreeViewItem

@dpaulino
Copy link
Contributor Author

By the way, it may be possible to close this issue if this question could be answered: #1154

@kaiguo
Copy link
Contributor

kaiguo commented Aug 13, 2019

By the way, it may be possible to close this issue if this question could be answered: #1154

I commented in #1154, closing this one.

@kaiguo kaiguo closed this as completed Aug 13, 2019
@jevansaks jevansaks added the team-Controls Issue for the Controls team label Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TreeView bug Something isn't working team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

4 participants