How to drag ListBoxItem?

Lucky B.C 51 Reputation points
2022-08-12T10:29:58.443+00:00

Hi, I can not drag any ListBoxItem from a ListBox to another ListBox. I tried to search in the internet, but the ppl answered for WPF or using only ListView instead or I have to set the content Grid's CanDrag to True.

Is it impossible to drag only a ListBoxItem? But can someone explain why we could drag a ListViewItem of ListView?

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,218 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,681 Reputation points Microsoft Vendor
    2022-08-22T06:44:32.53+00:00

    Hello,

    Welcome to Microsoft Q&A!

    How to drag ListBoxItem?

    To enable dragging on an element, set its CanDrag property to true. In your scenario, you need to set the CanDrag property of the root element of the Content of ListBoxItem.

    I've made a simple demo about this so you could understand it better:

                Grid grid = new Grid();  
                grid.CanDrag = true;  
      
                TextBlock block = new TextBlock();  
                block.Text = "123";  
                grid.Children.Add(block);  
      
                ListBoxItem item = new ListBoxItem();  
                item.Content = grid;  
                MyListBox.Items.Add(item);  
    

    The Grid is the root element of the Content of ListBoxItem, so make it draggable by enable the CanDrag property of the Grid. Then you could drag the Item.

    Please check the document Drag and drop for more information about how to handle the drag and drop event.

    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