You need to be able to add images to the application by dragging from the file system

Grid turned on AllowDrop . But how to add drag and drop images to the collection? Because Those examples that I have seen add images only to codebeehind.

Wrote method in control

  private void Grid_DragOver(object sender, DragEventArgs e) { e.AcceptedOperation = DataPackageOperation.Copy; if (e.DragUIOverride != null) { e.DragUIOverride.Caption = "Add file"; e.DragUIOverride.IsContentVisible = true; } } 

XAML

 <Grid Width="2560" Height="1600" Margin="0,0,0,0" AllowDrop="True" Drop=" " DragOver="Grid_DragOver" VerticalAlignment="Center" HorizontalAlignment="Center"> <Image Width="525" Height="331" Tapped="Image_Tapped" Source="{x:Bind Image}" Margin="{x:Bind Margins}" DoubleTapped="Image_DoubleTapped" RightTapped="Image_RightTapped" ManipulationDelta="Image_ManipulationDelta" ManipulationMode="TranslateX, TranslateY,Rotate,Scale"> <Image.RenderTransform> <CompositeTransform/> </Image.RenderTransform> </Image> 

The Drop method is sent to the team on the VM, there, in theory, I should add the received pictures to the ObservableCollection in which they are stored. (The application should have 2 types of loading pictures, through picker and drop). I do not understand how I get them there. I can not with the View in the VM to pass the argument DragEventArgs e or can I?

    1 answer 1

    Implemented the Drop method in control

      private async void Grid_Drop(object sender, DragEventArgs e) { if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var items = await e.DataView.GetStorageItemsAsync(); for (int i = 0; i < items.Count; i++) { ViewModelLocator.ManipulatorInstance.TempFiles.Add(items[i] as StorageFile); } ViewModelLocator.ManipulatorInstance.DropFilesExecute(); } } 

    In the loop, I threw it on the VM in the List<StorageFile> and called the method with the VM, which already adds to the collection

    • Upd, since the codebahind is not good, wrote Dependency Property and Behavior , I receive the data there and pass it to the command on the ViewModel - SmiLe