title: Drop Event Listbox
body: <p>I am trying to add the contents of my textfile to listbox. Here is the code :-</p>
  
  <p>This is my MainPage.xaml :-</p>
  
  <pre><code>&lt;UserControl
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:ec="http://schemas.microsoft.com/expression/2010/controls" x:Class="MVVMDemo.MainPage"
               xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
               xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
               mc:Ignorable="d"
               Height="300"
               Width="300"
               &gt;
  
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="Skins/MainSkin.xaml" /&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
              &lt;DataTemplate x:Key="DataTemplate1"&gt;
                  &lt;Grid&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="0.579*"/&gt;
                          &lt;ColumnDefinition Width="0.421*"/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;TextBlock Margin="0,0,0,4" TextWrapping="Wrap" Text="{Binding ContactName}" d:LayoutOverrides="Width, Height"/&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;UserControl.DataContext&gt;
          &lt;Binding Path="Main" Source="{StaticResource Locator}"/&gt;
      &lt;/UserControl.DataContext&gt;
  
      &lt;Grid x:Name="LayoutRoot" AllowDrop="True" UseLayoutRounding="True"&gt;
          &lt;i:Interaction.Triggers&gt;
              &lt;i:EventTrigger EventName="Drop"&gt;
                  &lt;cmd:EventToCommand  Command="{Binding DragCustomerCommand}" PassEventArgsToCommand="True"/&gt;
              &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
          &lt;ListBox x:Name="lstCustomersName" Margin="8" ItemsSource="{Binding Customers}" ItemTemplate="{StaticResource DataTemplate1}" FontSize="24" FontFamily="Arial Unicode MS" FontWeight="Bold"&gt;
  
              &lt;ListBox.Effect&gt;
                  &lt;DropShadowEffect/&gt;
              &lt;/ListBox.Effect&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>This is MainViewModel.cs :-</p>
  
  <pre><code>using GalaSoft.MvvmLight;
  using System.Collections.ObjectModel;
  using System.Windows;
  using GalaSoft.MvvmLight.Command;
  using System.IO;
  
  namespace MVVMDemo.ViewModel
  {
  
      public class MainViewModel : ViewModelBase
      {
          private RelayCommand&lt;DragEventArgs&gt; dragCustomerCommand = null;
          private RelayCommand&lt;DragEventArgs&gt; DragCustomerCommand
          {
              get
              {
                  return dragCustomerCommand;
              }
              set
              {
                  dragCustomerCommand = value;
  
              }
          }
  
          public string Welcome
          {
              get
              {
                  return "Welcome to MVVM Light!!!This is my firstDemo";
              }
          }
          private ObservableCollection&lt;CustomersServiceRef.Customer&gt; customers;
          public ObservableCollection&lt;CustomersServiceRef.Customer&gt; Customers
          {
              get
              {
                  return customers;
              }
              set
              {
                  customers = value;
                  RaisePropertyChanged("Customers");
              }
          }
  
          public MainViewModel()
          {
              dragCustomerCommand = new RelayCommand&lt;DragEventArgs&gt;(e =&gt; {
                  MessageBox.Show(e.Data.ToString());
                  if (e.Data == null)
                      return;
                  var files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
                  var file = files[0];
                  using (var stream = file.OpenRead())
                  {
                      using(var reader = new StreamReader(stream)){
                           var Customer = new CustomersServiceRef.Customer() { ContactName = reader.ReadLine()};
                           Customers.Insert(0, Customer);
                      }
  
                  }
              });
              var CustomerService = new CustomersServiceRef.CustomersServiceClient();
              if (IsInDesignMode)
              {
                  // Code runs in Blend --&gt; create design time data.
              }
              else
              {
                  CustomerService.GetCustomersCompleted += (s, e) =&gt;
                  {
                      if (e.Error == null)
                      {
                          Customers = new ObservableCollection&lt;CustomersServiceRef.Customer&gt;(e.Result);
                      }
                  };
                  CustomerService.GetCustomersAsync();
  
              }
          }
  
  
      }
  }
  </code></pre>
  
  <p>Why is the drop event not working on my grid? It accepts the textfile but the command doesn't fire and i don't get any messagebox.</p>
  
  <p>Thanks in advance :)</p>