如何使用ICommand从头开始拖放MVVM

时间:2012-05-04 13:13:46

标签: wpf vb.net mvvm drag-and-drop

在不使用MVVMLight的情况下,我需要使用mvvm优先使用Icommands和交互触发器将照片拖放到列表框中,但是如果我使用命令那么我不知道要传递什么命令参数?有任何想法吗?感谢。

这是我尝试的一些想法:

         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

结束班

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>       

1 个答案:

答案 0 :(得分:1)

我认为仅使用交互触发器是不可能的,因为您无法传递EventArgs。为什么不留mvvm光?它内置于那里

编辑:也许你可以找到this也很有用