C# 添加拖动&;随波逐流
我需要修改一个使用WPF、MVVM和行为进行事件处理的桌面应用程序。我有一个任务来实现按钮的拖放。如果用户按下按钮,它将弹出一个文件保存窗口,但如果用户单击并拖动它,它将显示一个文件图标,并让用户将其放入浏览器窗口中保存 我已经添加了名称空间:C# 添加拖动&;随波逐流,c#,wpf,mvvm,desktop-application,attachedbehaviors,C#,Wpf,Mvvm,Desktop Application,Attachedbehaviors,我需要修改一个使用WPF、MVVM和行为进行事件处理的桌面应用程序。我有一个任务来实现按钮的拖放。如果用户按下按钮,它将弹出一个文件保存窗口,但如果用户单击并拖动它,它将显示一个文件图标,并让用户将其放入浏览器窗口中保存 我已经添加了名称空间: xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviors="clr-namespace:MyApplication.Desktop.Clien
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:behaviors="clr-namespace:MyApplication.Desktop.Client.Behaviors"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:command="http://www.galasoft.ch/mvvmlight"
我还在按钮中添加了XAML代码:
<Button Grid.Column="2"
Command="{Binding SaveAttachmentCommand}"
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource boolToVisibilityConverter}}"
Style="{StaticResource AttachmentSaveButtonStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<command:EventToCommand Command="{Binding LeftMouseButtonDownCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<i:Interaction.Behaviors>
<behaviors:FrameworkElementDragBehavior>
</behaviors:FrameworkElementDragBehavior>
</i:Interaction.Behaviors>
</Button>
但我不知道如何告诉行为类(FrameworkElementDragBehavior)处理哪些事件以及如何处理它们(调用哪些函数)
我已经阅读了一些教程,但仍然感到困惑。两个月前我不得不用MVVM进行拖放操作 经过一些研究,个人认为,实现这一目标的最佳方法是使用“DragDrop”库。 它非常简单,非常适合您的需求。 例如,在树视图中:
<TreeView ItemsSource="{Binding LstCat}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}">
//Treeview Structure
</TreeView>
如果您感兴趣,以下是图书馆的链接:
public void DragOver(IDropInfo dropInfo)
{
if (dropInfo.Data is Category && dropInfo.TargetItem is Rubrique)
{
return;
}
dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
dropInfo.Effects = DragDropEffects.Move;
}