How to make certain TreeviewItem undragable

Renee GA 41 Reputation points
2022-09-15T23:23:49.537+00:00

I'm working with microsoft.ui.xaml treeview, what I wanted to do is keep some of the 'root folders' static in the treeview and unable to be dragged and/or reordered, while other treeview items can be freely moved and reordered between those root folders.

I currently cannot find a way to prevent a treeview item to be able to drag while having the whole treeview set to be CanReorderItems=True. Setting individual TreeviewItem to be CanDrag = False seems does not work.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,551 Reputation points Microsoft Vendor
    2022-09-16T03:27:03.02+00:00

    Hello,

    Welcome to Microsoft Q&A!

    How to make certain TreeviewItem undragable

    Currently, there is no API that could directly disable the drag function of a TreeViewItem. But there is a workaround that we could stop the drag event for the certain TreeViewItem by handling the TreeView.DragItemsStarting Event. This event happens when a drag operation involves one of the items. We could get the data of the dragged Item and check if this is the one we need to stop. If it is, stop the event by setting the Cancel property of the EventArgas to true.

    I've made a simple demo about this and you could check the code and adjust it to your scenario.

    XAML:

            <muxc:TreeView x:Name="DessertTree"   
                            CanDragItems="True"  
                           CanReorderItems="True"  
                           DragItemsStarting="DessertTree_DragItemsStarting"  
                           >  
                <muxc:TreeView.RootNodes>  
                    <muxc:TreeViewNode Content="Flavors" IsExpanded="True">  
                        <muxc:TreeViewNode.Children>  
                            <muxc:TreeViewNode Content="Vanilla"/>  
                            <muxc:TreeViewNode Content="Strawberry"/>  
                            <muxc:TreeViewNode Content="Chocolate"/>  
                        </muxc:TreeViewNode.Children>  
                    </muxc:TreeViewNode>  
      
                    <muxc:TreeViewNode Content="Toppings">  
                        <muxc:TreeViewNode.Children>  
                            <muxc:TreeViewNode Content="Candy">  
                                <muxc:TreeViewNode.Children>  
                                    <muxc:TreeViewNode Content="Chocolate"/>  
                                    <muxc:TreeViewNode Content="Mint"/>  
                                    <muxc:TreeViewNode Content="Sprinkles"/>  
                                </muxc:TreeViewNode.Children>  
                            </muxc:TreeViewNode>  
                            <muxc:TreeViewNode Content="Fruits">  
                                <muxc:TreeViewNode.Children>  
                                    <muxc:TreeViewNode Content="Mango"/>  
                                    <muxc:TreeViewNode Content="Peach"/>  
                                    <muxc:TreeViewNode Content="Kiwi"/>  
                                </muxc:TreeViewNode.Children>  
                            </muxc:TreeViewNode>  
                            <muxc:TreeViewNode Content="Berries">  
                                <muxc:TreeViewNode.Children>  
                                    <muxc:TreeViewNode Content="Strawberry"/>  
                                    <muxc:TreeViewNode Content="Blueberry"/>  
                                    <muxc:TreeViewNode Content="Blackberry"/>  
                                </muxc:TreeViewNode.Children>  
                            </muxc:TreeViewNode>  
                        </muxc:TreeViewNode.Children>  
                    </muxc:TreeViewNode>  
                </muxc:TreeView.RootNodes>  
            </muxc:TreeView>  
    

    MainPage:

            private void DessertTree_DragItemsStarting(MUXC.TreeView sender, MUXC.TreeViewDragItemsStartingEventArgs args)  
            {  
                   
                if (args.Items.Count == 1)  
                {  
                    //get the item   
                    var str = args.Items.FirstOrDefault().ToString();  
                    // a simple check to the item data  
                    if (str.Equals("Peach")|| str.Equals("Strawberry") || str.Equals("Chocolate"))   
                    {  
                        //stop the event  
                        args.Cancel = true;  
                    }  
                }  
            }  
    

    In this sample, you can't drag Peach, Strawberry and Chocolate.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful