1
votes

I have a listview which property SelectionMode is set to Extended.

Also I have a trash button. I am trying to perform delete operation on selected items on listview when user drags one or multiple selected items (using shift key if more than one item) and drop over delete button.

I have implemented it with a single selected item in listview and it works. Now I am trying to do the same with multiple selected items in listview without success.

Below code works when one single item is selected in listview and user drag and drop it from listview to delete button:

    <ListView Margin="10" Name="lvUsers" ItemsSource="{Binding Path=Items}" SelectionMode="Extended">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem">                                                               
                                <EventSetter Event="MouseMove"  Handler="lstItems_MouseMove" />
                            </Style>
                        </ListView.ItemContainerStyle>

                        <!-- other stuff -->

    </Listview>

    <Button  AllowDrop="True"  Drop="btnDelete_Drop" Height="64" Width="64" Margin="10" Click="BtnDelete_Click" Content="Delete"/>

and the code-behind:

    private void lstItem_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (e.Source != null)
            {
                DataModel selectedItem = (DataModel)lvUsers.SelectedItem;

                DragDrop.DoDragDrop(lvUsers, selectedItem, DragDropEffects.Move);                  
            }
        }
    }


    private void btnDelete_Drop(object sender, DragEventArgs e)
    {
        Type myType = typeof(DataModel);
        string modelns = myType.FullName;

        DataModel selectedItem = e.Data.GetData(modelns) as DataModel;
        MessageBox.Show(selectedItem.Name);

    }

Each listviewitem on listview is of data type below:

   public class DataModel
    {
        public string Name
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }

        public string Mail
        {
            get;
            set;
        }
    }

As an example, when user drops the dragged listviewitem over delete button I show a message box with the name of the person.

How can I do the same but instead of dragging and dropping one single item from listview, do the same for multiple selected listviewitems? Once dropped, within btnDelete_Drop I want to iterate over all the listviews items dropped and do some stuff.

1

1 Answers

1
votes

I have just implemented a solution that works. I have replaced methods lstItem_MouseMove and btnDelete_Drop in code-behind by these ones:

private void lstItem_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        if (e.Source != null)
        {
                List<DataModel> myList = new List<DataModel>();
                foreach (DataModel Item in lvUsers.SelectedItems)
                {
                    myList.Add(Item);
                }                 

                DataObject dataObject = new DataObject(myList);                    
                DragDrop.DoDragDrop(lvUsers, dataObject, DragDropEffects.Move);               
        }
    }
}


private void btnDelete_Drop(object sender, DragEventArgs e)
{
        Type myType = typeof(List<DataModel>);

        List<DataModel> selectedItems = e.Data.GetData(myType) as List<DataModel>;

        string hello = "Hello ";
        foreach (DataModel dm in selectedItems)
        {
            hello = hello + ", " + dm.Name;
        }

        MessageBox.Show(hello);
}

This is working but mayber there are any other better solution. Any suggestion will be welcome.