2017-02-06 110 views
1

我需要修改一個使用WPF,MVVM和Behaviors進行事件處理的桌面應用程序。我有一個任務來實現拖動&刪除按鈕。如果用戶按下按鈕,它將彈出一個文件保存窗口,但如果用戶單擊並拖動它,它應該顯示一個文件圖標,並讓用戶將其放入資源管理器窗口並將其保存在那裏。拖放行爲

我已經添加命名空間:

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 )處理哪些事件以及如何處理它們(調用哪些函數)。

我讀過一些教程,但我仍然感到困惑。

回答

0

我不得不在兩個月前使用MVVM進行拖放操作。

經過一番研究後,personnaly的最佳實現方式是與「GongSolutions DragDrop」庫一起工作。 它非常簡單,非常適合您要找的東西。 例如,在一個TreeView:

<TreeView ItemsSource="{Binding LstCat}" 
       dd:DragDrop.IsDragSource="True" 
       dd:DragDrop.IsDropTarget="True" 
       dd:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}"> 
    //Treeview Structure 

    </TreeView> 

從那裏,你可以做在TreeView拖動&下降。您也可以添加一個dragAdorner(當您拖動某物時指向您指針旁邊的圖像)

在viewModel中,您可以通過實現庫的附帶界面來指定拖動或拖放的行爲。這樣你就可以訪問你拖動的數據。 例如:

public void DragOver(IDropInfo dropInfo) 
    { 
     if (dropInfo.Data is Category && dropInfo.TargetItem is Rubrique) 
     { 
      return; 
     } 

     dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight; 
     dropInfo.Effects = DragDropEffects.Move; 
    } 

這裏是圖書館的,如果你有興趣的鏈接: https://github.com/punker76/gong-wpf-dragdrop