0

在不使用 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>       
4

1 回答 1

1

我认为仅使用交互触发器是不可能的,因为您无法传递 EventArgs。为什么不继续使用 mvvm light?它建在那里

编辑:也许你会发现也有帮助

于 2012-05-04T13:21:33.147 回答