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