Click or drag to resize
Code Example - MapView_DragDropEvents

Demonstrates creating a CsvLayer by performing a Drag-and-Drop from a file on the local disk in File Explorer to the MapView.

Code Example
Map View Drag Drop Events

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="MapView_DragDropEvents.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">

    <Grid x:Name="LayoutRoot">

        <!-- Define a SimpleRenderer with a SimpleMarkerSymbol that will be used the symbology for a CsvLayer 
        added via code-behind. -->
        <Grid.Resources>
            <esri:SimpleRenderer x:Key="mySimpleRenderer">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleMarkerSymbol Color="Red" Style="Circle" Size="12"/>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
        </Grid.Resources>

        <StackPanel Orientation="Vertical">

            <!-- TextBlock to provide the instructions on how to use the sample code. It will be 
                populated with instructions in the code-behind when the application loads. -->
            <TextBlock Height="81" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

            <StackPanel Orientation="Horizontal">

                <!-- Add a MapView Control. Enable the Events of DragEnter, DrageLeave, and Drop. This will 
                allow a user to Drag-and-Drop a .csv file from the File Explorer on disk into the MapView and 
                have it display as a CsvLayer. Whenever you use the various Drag-and-Drop events, it is necessary 
                to set the AllowDrop Property to be True. -->
                <esri:MapView x:Name="MapView1" Width="600" Height="480" VerticalAlignment="Top" Margin="2,2,2,2"
                              DragEnter="MapView1_DragEnter" DragLeave="MapView1_DragLeave"  Drop="MapView1_Drop" 
                              AllowDrop="True" >

                    <!-- A Map. Set the initial extent (via the InitialViewpoint Property) to be the continental US. -->
                    <esri:Map  x:Name="Map1" InitialViewpoint="-15219969,2609636,-6232883,6485365">

                        <!-- Add a backdrop ArcGISTiledMapServiceLayer-->
                        <esri:ArcGISTiledMapServiceLayer ID="World_Topo_Map" 
                      ServiceUri="http://services.arcgisonline.com/arcgis/rest/services/world_topo_map/MapServer" />
                    </esri:Map>

                </esri:MapView>

                <!-- Add some controls to allow the user to see when the various Drag-and-Drop Events have fired. -->
                <StackPanel Orientation="Vertical">
                    <Label Content="DragDrop Events:" FontSize="14"/>
                    <TextBlock x:Name="TextBlockDragDropEvents" Width="187" Height="456"/>
                </StackPanel>

            </StackPanel>
        </StackPanel>
    </Grid>

</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "MapView_DragDropEvents.MainWindow" to be just "MainWindow".

// INSTRUCTIONS:
// 
// (1) Copy the following ASCII text into Notepad and save the file as C:\TestData\Cities.csv:
// ID,Lat,Long,CityName,Population
// 1,40.714,-74.006,New York City,8244910
// 2,34.0522,-118.244,Los Angeles,3819702
// 3,41.878,-87.636,Chicago,2708120
// 4,29.763,-95.363,Houston,2099451
// 5,39.952,-75.168,Philadelphia,1526006
// 6,29.423,-98.493,San Antonio,1327407
// 7,32.715,-117.156,San Diego,1326179
// 8,32.782,-96.815,Dallas,1223229
// 9,37.228,-119.228,San Jose,945942
// 10,30.331,-81.655,Jacksonville,821784
// 
// (2) Copy the following ASCII text into Notepad and save the file as C:\TestData\Garbage.bad:
// BLAH BLAH BLAH.
// THIS FILE CONTAINS GARBAGE.
// 
// (3) Run the application and following the instructions in the TextBox.

namespace MapView_DragDropEvents
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "Open a File Explorer window and navigate to the C:\\TestData directory. Drag the Garbage.bad file onto the MapView Control and then " + 
                "drag it back to the File Explorer window (don't drop the Garbage.bad file onto the MapView at this point). You should see the several event messages " + 
                "display in the TextBlockDragDropEvents control. Then drag the Cities.csv file onto the MapView Control and drop it onto the MapView Control. You " + 
                "should see CsvLayer graphics added to the Map for the top 10 largest cities in the US.";
        }

        private void MapView1_DragEnter(object sender, System.Windows.DragEventArgs e)
        {
            // Display to the user that the MapView.DrageEnter Event has fired.
            TextBlockDragDropEvents.Text = TextBlockDragDropEvents.Text + "DragEnter occurred" + System.Environment.NewLine;

            // Get the name of the file that is being dragged from the File Explorer into the MapView Control. Use the exact string: "FileNameW" as the parameter 
            // of the e.Data.GetData Method to obtain the full path/filename of the file being dragged from the File Explorer to the MapView. The actual return 
            // value from the e.Data.GetData Method is an Array of String. Some starter reference materials for Drag-and-Drop can be found in the following documents: 
            // "How To: Add a Drag-and-Drop Handler" (by Microsoft): http://msdn.microsoft.com/en-us/library/ff793459.aspx 
            // "Enable Your Windows Forms Applications to Drag-and-Drop Data Objects" (non Microsoft): http://www.codemag.com/Article/0407031
            string[] myObject = (string[])e.Data.GetData("FileNameW", true);

            if (myObject != null)
            {
                // Because the myObject is an Array of Strings, get the first element in the array, this will be full path/filename of the file being dragged from the File Explorer.
                string fullpath = (string)myObject[0];

                // Determine the file extension of the file being dragged into the MapView.
                if (fullpath.EndsWith(".csv"))
                {
                    // We have a .csv file. Let the user know this is a valid type that can be dropped into the MapView.
                    TextBlockDragDropEvents.Text = TextBlockDragDropEvents.Text + "Valid File Type" + System.Environment.NewLine;
                }
                else
                {
                    // We have some unknown file type. Notify the user this is not an acceptable file type that can be dropped into the MapView.
                    TextBlockDragDropEvents.Text = TextBlockDragDropEvents.Text + "Not Valid File Type" + System.Environment.NewLine;
                }
            }
        }

        private void MapView1_DragLeave(object sender, System.Windows.DragEventArgs e)
        {
            // Inform the user that the use left the MapView with a Dragged item from the File Explorer. NOTE: the MapView.Drop Event is not fired
            // if you drag the item out of the MapView. 
            TextBlockDragDropEvents.Text = TextBlockDragDropEvents.Text + "DragLeave occurred" + System.Environment.NewLine + System.Environment.NewLine;
        }

        private void MapView1_Drop(object sender, System.Windows.DragEventArgs e)
        {
            // Inform the user they have dropped the item from the File Explorer into the MapView.  NOTE: this does not mean that the item will automatically be
            // added to the MapView. In this code example, we will only be adding .csv files as CSVLayer objects into the MapView.
            TextBlockDragDropEvents.Text = TextBlockDragDropEvents.Text + "Drop occurred" + System.Environment.NewLine;

            // Get the object (Array of String) that has the path/filename of the file that is being dragged from the File Explorer into the MapView Control.
            string[] myObject = (string[])e.Data.GetData("FileNameW", true);

            if (myObject != null)
            {
                // Because the myObject is an Array of String, get the first element in the array, this will be full path/filename of the file being dragged from the File Explorer.
                string fullpath = myObject[0];

                // Determine the file extension ends with a ".csv".
                if (fullpath.EndsWith(".csv"))
                {
                    // Create a CsvLayer. 
                    Esri.ArcGISRuntime.Layers.CsvLayer myCsvLayer = new Esri.ArcGISRuntime.Layers.CsvLayer();

                    // Set the ServiceUri Property to the path/filename of the .csv file on local disk. 
                    myCsvLayer.ServiceUri = fullpath;

                    // Set the ID of the CsvLayer.
                    myCsvLayer.ID = "US_Cities_Top_5";

                    // Get a SimpleRenderer that was defined in XAML and apply it to the CsvLayer. NOTE: it is imperative that a Renderer be set on the 
                    // CsvLayer or else no visual elements will be drawn in the Map.
                    myCsvLayer.Renderer = LayoutRoot.Resources["mySimpleRenderer"] as Esri.ArcGISRuntime.Symbology.SimpleRenderer;

                    // Add the CsvLayer to the Map.
                    MapView1.Map.Layers.Add(myCsvLayer);

                    // Add an ending linefeed/carriage return to make the output format look more readable if multiple Drag-and-Drop operations are performed.
                    TextBlockDragDropEvents.Text = TextBlockDragDropEvents.Text + System.Environment.NewLine;
                }
            }
        }    

    }
}