WPF Drag and Drop - Получить оригинальную информацию об источнике от DragEventArgs - PullRequest
4 голосов
/ 23 декабря 2010

Я пытаюсь написать функциональность Drag and Drop, используя MVVM, что позволит мне перетаскивать PersonModel объекты из одного ListView в другой.

Это почти работает, но мне нужно иметь возможность получить ItemsSource исходного ListView из DragEventArgs, который я не могу понять, как это сделать.

private void OnHandleDrop(DragEventArgs e)
{
    if (e.Data != null && e.Data.GetDataPresent("myFormat"))
    {
        var person = e.Data.GetData("myFormat") as PersonModel;
        //Gets the ItemsSource of the source ListView
        ..

        //Gets the ItemsSource of the target ListView and Adds the person to it
        ((ObservableCollection<PersonModel>)(((ListView)e.Source).ItemsSource)).Add(person);
    }
}

Любая помощь будет принята с благодарностью.

Спасибо!

1 Ответ

4 голосов
/ 23 декабря 2010

Я нашел ответ в другой вопрос

Способ сделать это - передать исходный ListView в метод DragDrow.DoDragDrop, т.е.

В методе, который обрабатывает PreviewMouseMove для ListView do-

private static void List_MouseMove(MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        if (e.Source != null)
        {
            DragDrop.DoDragDrop((ListView)e.Source, (ListView)e.Source, DragDropEffects.Move);
        }
    }
}

и затем в методе OnHandleDrop измените код на

private static void OnHandleDrop(DragEventArgs e)
{
    if (e.Data != null && e.Data.GetDataPresent("System.Windows.Controls.ListView"))
    {
        //var person = e.Data.GetData("myFormat") as PersonModel;
        //Gets the ItemsSource of the source ListView and removes the person
        var source = e.Data.GetData("System.Windows.Controls.ListView") as ListView;
        if (source != null)
        {
            var person = source.SelectedItem as PersonModel;
            ((ObservableCollection<PersonModel>)source.ItemsSource).Remove(person);

            //Gets the ItemsSource of the target ListView
            ((ObservableCollection<PersonModel>)(((ListView)e.Source).ItemsSource)).Add(person);
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...