Select to view content in your preferred language

Dockpane with listed files: drag&drop method to add files to map does not work. where's the issue?

709
3
09-06-2023 11:31 PM
nadja_swiss_parks
Occasional Contributor III

Hi - I'm new to this SDK and C#. I'm trying to code a dockpane which lists all rasters of a given folder. this works. now I would like to add the listed rasters with drag&drop (or right-click - add) to the current map. unfortunately I don't get it to work. it doesn't throw any errors, so I have no clue what's missing... can someone help me please?

here my xaml:

        <ListView Name="FileListView" SelectionChanged="FileListView_SelectionChanged"  AllowDrop="True" Drop="FileListView_Drop" Height="Auto" Margin="0,28,0,113" Grid.Row="1">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Filename" Width="250">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Refresh" Click="OnRefreshButtonClicked" HorizontalAlignment="Left" Margin="0,3,0,0" Grid.Row="1" VerticalAlignment="Top"/>

 

here my xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;


namespace dockpane_parcs
{
    /// <summary>
    /// Interaction logic for Dockpane1View.xaml
    /// </summary>
    public partial class Dockpane1View : UserControl
    {

        private ObservableCollection<string> fileNames;

        public Dockpane1View()
        {
            InitializeComponent();

            fileNames = new ObservableCollection<string>();
            FileListView.ItemsSource = fileNames;

            FileListView.Drop += FileListView_Drop;
        }

        private void OnRefreshButtonClicked(object sender, RoutedEventArgs e)
        {
            // Define the list of allowed file extensions
            string[] allowedExtensions = { ".tif", ".TIF", ".tiff", ".TIFF" };

            // Update the list of files in a folder
            string folderPath = @"C:\XXXX\YYYYY"; // Replace with your folder path

            try
            {

                // Get a list of files with the allowed extensions from the folder
                var files = Directory.GetFiles(folderPath)
                                     .Where(file => allowedExtensions.Contains(System.IO.Path.GetExtension(file)))
                                     .ToList();

                fileNames.Clear();
                foreach (string file in files)
                {
                    fileNames.Add(System.IO.Path.GetFileName(file));
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }

        private void FileListView_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Get the dropped files as an array of strings
                string[] droppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);

                // Filter for allowed file extensions
                string[] allowedExtensions = { ".tif", ".TIF", ".tiff", ".TIFF" };
                var validFiles = droppedFiles.Where(file => allowedExtensions.Contains(System.IO.Path.GetExtension(file)));

                // Add the valid files to the ObservableCollection
                foreach (string file in validFiles)
                {
                    fileNames.Add(System.IO.Path.GetFileName(file));
                }
            }
        }

        private void FileListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }
}
0 Kudos
3 Replies
BerendVeldkamp
Occasional Contributor II

Could you be a bit more specific about what exactly does not work? For instance, is the Drop event called at all?

 

That said, I think you need to initiate drag-drop in the MouseMove event first. See here for an example (it is for .NET Framework, but Core will probably be very similar)

0 Kudos
nadja_swiss_parks
Occasional Contributor III

Thank you for your quick response. Honestly, I'm not sure.  I don't see the mouse change from the arrow to the forbidden-sign or the arrow with the square. So I guess it's not called?

thanks for the hint with the MouseMove, but I don't get what exactly should I do with that? 

0 Kudos
BerendVeldkamp
Occasional Contributor II

Did you read the article I linked to? It will tell you exactly what to do

0 Kudos