TMan
TMan

Reputation: 4122

How to Drag and Drop For listbox from scratch MVVM using ICommand

Without using MVVMLight, I need to implement drag and drop for photos onto a listbox using mvvm using Icommands preferably and interaction triggers, however if i use commands then I don't know what to pass in for a command parameter? Any ideas? Thanks.

Heres some ideas I tried:

         Public Property ImageList As New ObservableCollection(Of ListBoxItem)
Public Property AddImageCommand As ICommand = New Adjuster.DelegateCommand(AddressOf addImage)

Public Property DropCommand As ICommand = New Adjuster.DelegateCommand(AddressOf dropImage)

'Private Shared ReadOnly PreviewDropCommandProperty As DependencyProperty =
' DependencyProperty.RegisterAttached("PreviewDropCommand", GetType(ICommand), GetType(TestDocumentViewModel), New PropertyMetadata(PreviewDropCommandPropertyChangedCallBack))

Private Sub addImage()
    Dim dlg As New Microsoft.Win32.OpenFileDialog()
    dlg.ShowDialog()
End Sub

Private Sub dragImage(parm As Object)
    Throw New NotImplementedException
End Sub


Private Sub dropImage(e As Object)
    Dim droppedFilePaths As String() = TryCast(e.Data.GetData(DataFormats.FileDrop, True), String())

    For Each droppedFilePath As String In droppedFilePaths

        Dim fileItem As New ListBoxItem()

        fileItem.Content = System.IO.Path.GetFileNameWithoutExtension(droppedFilePath)
        fileItem.ToolTip = droppedFilePath

        ImageList.Add(fileItem)
        RaisePropertyChanged("ImageList")
        'DropListBox.Items.Add(fileItem)
    Next

End Sub

'Private Shared Function GetPreviewDropCommand(inUIElement As UIElement) As ICommand
'    Return DirectCast(inUIElement.GetValue(PreviewDropCommandProperty), ICommand)
'End Function

'Private Shared Sub PreviewDropCommandPropertyChangedCallBack(inDependencyObject As DependencyObject, inEventArgs As DependencyPropertyChangedEventArgs)
'    Dim uiElement As UIElement = TryCast(inDependencyObject, UIElement)
'    If uiElement Is Nothing Then
'        Return
'    End If

'    uiElement.Drop += Function(sender, args) Do
'    GetPreviewDropCommand(uiElement).Execute(args.Data)
'    args.Handled = True

'End Sub


'End Sub

End Class

wpf

    <ListBox ItemsSource="{Binding ImageList}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" AllowDrop="True" Name="ImageListBox" >

            <i:Interaction.Triggers>
            <i:EventTrigger EventName="Drop">
                    <i:InvokeCommandAction Command="{Binding Path=DropCommand}" CommandParameter="{Binding }"  />
                    <!--<cmd:EventToCommand Command="{Binding Path=DropCommand}" PassEventArgsToCommand="True" />-->
                </i:EventTrigger>
          </i:Interaction.Triggers>
        </ListBox>       

Upvotes: 0

Views: 1103

Answers (1)

blindmeis
blindmeis

Reputation: 22435

i think its not possible with just the interaction triggers because you can not pass EventArgs. why not stay with mvvm light? its built in there

EDIT: maybe you can find this also helpful

Upvotes: 1

Related Questions