Reputation: 906
i am creating my first mvvm app in wpf, i came to a point where i need to drag and drop files into a ListBox, i couldn't see any other way to do it but use the DragEnter and the Drop events, then add the dropped files to my collection by casting the DataContext of the view to my ViewModel and the accessing the collection from there, and that is working really great.
something like this in my View in the drop event
(this.DataContext as ImageViewModel).ToUpload.Add(new ImageModel() { localfilelocation = fi.FullName })
but is there anyother way do it?
Upvotes: 0
Views: 600
Reputation: 3764
To be honest, dragging and dropping is probably one of those activities that MVVM finds most difficult to fit naturally into it's paradigm. However, the most common way of doing this is by using Attached Behaviours. A DragDropBehaviour
, sub-classing Behavior<T>
will allow you to hook into those events on the ListBox
and have the advantage of being totally re-usable.
A good example of this can be found here.
Upvotes: 4