【问题标题】:Can't get drag and drop events to fire无法触发拖放事件
【发布时间】:2018-12-21 05:11:47
【问题描述】:

我今天一直在桌子上敲打我的头,试图让我的拖放事件触发。拖放功能在界面上有效,但事件不会触发。我需要触发事件,以便使用对象的新顺序更新数据库。我做错了什么?

在下面的代码中,我在 'Drop' 和 'DragOver' 方法中放置了断点,但它们从未被命中。

XAML:

<Window x:Class="Reorder_item_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" 
         xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop">
    <Grid>
        <ListBox Grid.Column="1" SelectionMode="Extended" ItemsSource="{Binding MSPCollection}" 
                 dd:DragDrop.IsDragSource="True" Width="300" Margin="0,0,5,0" dd:DragDrop.IsDropTarget="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="#2ba3d5" Height="50" Width="280">
                        <TextBlock Text="{Binding Name}" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

c#:

public class MSP {
    public int Id { get; set; }
    public string Name { get; set; }        
}

class MainViewModel : IDropTarget
    {
        public ObservableCollection<MSP> MSPCollection { get; set; }

        public MainViewModel() {
            MSPCollection = new ObservableCollection<MSP>();

            MSPCollection.Add(new MSP() { 
                Id = 1,
                Name = "Anis Derbel"
            });

            MSPCollection.Add(new MSP()
            {
                Id = 2,
                Name = "Firas Mdimagh"
            });

            MSPCollection.Add(new MSP()
            {
                Id = 3,
                Name = "Khaled Jemni"
            });

            MSPCollection.Add(new MSP()
            {
                Id = 4,
                Name = "Sahbouch"
            });        
        }

        public void DragOver(IDropInfo dropInfo) {
            if (dropInfo.Data is MSP) {
                dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
                dropInfo.Effects = DragDropEffects.Move;
            }
        }

        public void Drop(IDropInfo dropInfo) {
            MSP msp = (MSP)dropInfo.Data;
            ((IList)dropInfo.DragInfo.SourceCollection).Remove(msp);
        }
    }

【问题讨论】:

    标签: c# wpf xaml drag-and-drop


    【解决方案1】:

    您还需要通过相应的附加属性设置 DropHandler:

    <ListBox ItemsSource="{Binding Collection}"
         dd:DragDrop.IsDragSource="True"
         dd:DragDrop.IsDropTarget="True"
         dd:DragDrop.DropHandler="{Binding}" />
    

    来自documentation

    【讨论】:

    • 这会触发 'DragOver' 和 'Drop' 方法,但随后拖放功能完全崩溃了。 :(
    • “崩溃”是什么意思?
    • 进入 {Binding} 后,我的断点命中,但界面上的拖放功能不再起作用。我拖动的项目会消失。
    • 这可能与此有关:((IList)dropInfo.DragInfo.SourceCollection).Remove(msp);
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2013-04-15
    • 2013-04-26
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多