From d2c73ae32820c32c5023d9e8ef924c47c7e0da64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Fri, 15 Jun 2018 17:02:31 +0200 Subject: [PATCH] Added drag and drop support --- src/MicroStationTagExplorer/MainWindow.xaml | 3 +- .../MainWindow.xaml.cs | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/MicroStationTagExplorer/MainWindow.xaml b/src/MicroStationTagExplorer/MainWindow.xaml index 0219859..0efbb17 100644 --- a/src/MicroStationTagExplorer/MainWindow.xaml +++ b/src/MicroStationTagExplorer/MainWindow.xaml @@ -8,7 +8,8 @@ Title="MicroStation Tag Explorer" Height="650" Width="1000" WindowStartupLocation="CenterScreen" WindowState="Normal" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display" SnapsToDevicePixels="True" - PreviewKeyDown="Window_PreviewKeyDown"> + PreviewKeyDown="Window_PreviewKeyDown" + AllowDrop="True" Drop="Window_Drop" DragEnter="Window_DragEnter"> diff --git a/src/MicroStationTagExplorer/MainWindow.xaml.cs b/src/MicroStationTagExplorer/MainWindow.xaml.cs index b16063c..20cfec0 100644 --- a/src/MicroStationTagExplorer/MainWindow.xaml.cs +++ b/src/MicroStationTagExplorer/MainWindow.xaml.cs @@ -201,6 +201,65 @@ namespace MicroStationTagExplorer } } + private void Window_Drop(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + { + var data = e.Data.GetData(DataFormats.FileDrop); + if (data != null && data is string[]) + { + IList files = null; + if (DataGridFiles.DataContext != null) + { + files = DataGridFiles.DataContext as IList; + } + else + { + files = new ObservableCollection(); + DataGridFiles.DataContext = files; + } + + var droppedFiles = data as string[]; + foreach (var droppedFile in droppedFiles) + { + System.IO.FileAttributes attributes = System.IO.File.GetAttributes(droppedFile); + if (attributes.HasFlag(System.IO.FileAttributes.Directory)) + { + var allFiles = System.IO.Directory.EnumerateFiles(droppedFile, "*.*", System.IO.SearchOption.AllDirectories) + .Where(s => s.EndsWith(".dwg") || s.EndsWith(".dgn")); ; + foreach (var allFile in allFiles) + { + var file = new File() + { + Path = allFile + }; + files.Add(file); + } + } + else + { + if (droppedFile.EndsWith(".dwg") || droppedFile.EndsWith(".dgn")) + { + var file = new File() + { + Path = droppedFile + }; + files.Add(file); + } + } + } + } + } + } + + private void Window_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + { + e.Effects = DragDropEffects.All; + } + } + private void FileAddFiles_Click(object sender, RoutedEventArgs e) { AddFiles();