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

Update TreeView to support blocking drag&drop for specific items #1646

Merged
merged 6 commits into from Nov 23, 2019

Conversation

kaiguo
Copy link
Contributor

@kaiguo kaiguo commented Nov 20, 2019

Description

Enable TreeView to block drag&drop on certain items.

Added checks for "handled" in drag&drop callbacks so developers can set "handled" to true to skip TreeView built-in drag drop logic.

Also added a "NewParent" property in TreeViewDragItemsCompletedEventArgs, which makes it easier to figure out the drop position.

Motivation and Context

Fix #381
Fix #1154

How Has This Been Tested?

Updated existing test to include "NewParent" property.

dev/TreeView/TreeView.cpp Outdated Show resolved Hide resolved
m_dragItemsStartingEventSource(*this, *treeViewArgs);
}

void TreeView::OnListControlDragItemsCompleted(const winrt::IInspectable& sender, const winrt::DragItemsCompletedEventArgs& args)
{
auto treeViewArgs = winrt::make_self<TreeViewDragItemsCompletedEventArgs>();
treeViewArgs->DragItemsCompletedEventArgs(args);
winrt::IInspectable newParent = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should try to avoid scratch variables if possible, I might suggest the following:

const auto newParent = [items = args.Items(), listControl = ListControl(), rootNode = m_rootNode.get()]()
{
    if (listControl && items && items.Size() > 0)
    {
        if (const auto draggedNode = listControl->NodeFromItem(items.GetAt(0)))
        {
            const auto parentNode = draggedNode.Parent();
            if (parentNode && parentNode != rootNode)
            {
                return listControl->ItemFromNode(parentNode);
            }
        }
    }
    return static_cast<winrt::IInspectable>(nullptr);
}();

Copy link
Member

@kmahone kmahone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@kaiguo
Copy link
Contributor Author

kaiguo commented Nov 23, 2019

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jevansaks jevansaks added the release note PR that we want to call out in the next release summary label Nov 23, 2019
@kaiguo kaiguo merged commit 4dd6b16 into master Nov 23, 2019
@kaiguo kaiguo deleted the user/kaiguo/treeview-drag-drop branch November 23, 2019 21:07
@AignerGames
Copy link

AignerGames commented Aug 27, 2020

Might be a dumb question, but I want to enable the general reorder logic but need some specific checks:
A: prevent drag/reorder child nodes if they are under a specific parent
B: don't allow node drops which are dragged from other parent nodes (which allow drag&drop) under this specific parent

It sounds like this change made it possible to do so, but I don't understand how I would implement it with this change, can some one give me a example or some hints?

@StephenLPeters
Copy link
Contributor

@AignerGames I think that the answer is to set the CanDrag and AllowDrop properties on that parent node to false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TreeView release note PR that we want to call out in the next release summary
Projects
None yet
5 participants