From 74df15a0bcf28b16b68ee73630cd65f997bc14c2 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Sat, 27 Jan 2024 12:35:24 +0000 Subject: [PATCH] added drag drop --- MP3joiner.csproj | 4 +-- MainWindow.xaml | 20 +++++++++--- MainWindow.xaml.cs | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/MP3joiner.csproj b/MP3joiner.csproj index aceddfb..f14519b 100644 --- a/MP3joiner.csproj +++ b/MP3joiner.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.0.0.59 - 1.0.0.59 + 1.0.0.68 + 1.0.0.68 logo (1).ico logo.png diff --git a/MainWindow.xaml b/MainWindow.xaml index c020108..09ae52b 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -9,6 +9,7 @@ TextElement.FontSize="13" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Auto" + Background="#0A4185" FontFamily="{DynamicResource MaterialDesignFont}"> @@ -70,16 +71,25 @@ + + HeadersVisibility="Column" + AllowDrop="True" + Drop="Window_Drop" + DragEnter="Window_DragEnter" + ItemsSource="{Binding FileList}" + dd:DragDrop.IsDragSource="True" + dd:DragDrop.IsDropTarget="True" + dd:DragDrop.DropHandler="{Binding DropHandler}"> diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 77c6918..caa4cc0 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -51,6 +51,84 @@ public MainWindow() #endregion Public Constructors #region Private Methods + private void Window_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + { + e.Effects = DragDropEffects.Copy; // Show copy cursor + } + else + { + e.Effects = DragDropEffects.None; // Show no-entry cursor + } + + e.Handled = true; + } + + private void Window_Drop(object sender, DragEventArgs e) + { + // Check if the drop is from an external source (Windows Explorer) + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + { + string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); + HandleFileDrop(files); + e.Handled = true; // Mark the event as handled + } + else + { + // If the drop is not from an external source, + // it might be an internal drag-and-drop for reordering. + // Do not set e.Handled to true here. + // This allows GongSolutions.Wpf.DragDrop to handle internal reordering. + } + } + + + private void HandleFileDrop(string[] files) + { + var viewModel = DataContext as YourViewModel; + if (viewModel == null) return; + var firstadded = true; + // Your existing logic for handling file drops + foreach (string file in files) + { + + // Add each file to your file list, for example: + // Assuming 'fileList' is your ObservableCollection that's bound to the UI + if (viewModel.FileList.Any() && viewModel.FileList.All(f => f.FilePath != file) && firstadded == true) + { + // Create a new instance of AddFilesDialog + var dialog = new AddFilesDialog(); + + // Set the main window as the owner of the dialog + dialog.Owner = this; + + // Show the dialog and wait for the user to close it + dialog.ShowDialog(); + + // Check the result of the dialog + if (dialog.DialogResult == "NewList") + { + viewModel.FileList.Clear(); // Clear the existing list in the view model + firstadded = false; + } + else if (dialog.DialogResult == "Cancel") + { + return; // Cancel the operation and return from the event handler + } + } + viewModel.FileList.Add(new FileInfo { FilePath = file }); + firstadded = false; + + } + } + + + private void btnclearList_Click(object sender, RoutedEventArgs e) + { + var viewModel = DataContext as YourViewModel; + viewModel.FileList.Clear(); + } // This method is used to animate a progress bar to zero value private void AnimateProgressBarToZero()