DataGridRow drag and drop behavior #10877
Replies: 5 comments 2 replies
-
That is really nice 👍 . One thing that comes into my mind when I see it is that a tooltip showing the dragged item could be useful for the user of the App. It's not mandatory though. |
Beta Was this translation helpful? Give feedback.
-
I copied your solution to use in my app hehehe! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Nice, one thing I should probably mentioned is that I found that it's not completely infallable: it's possible to initiate a drag such that it's no longer possible to determine the source datagrid accurately. I partially solved it by using a drag handler (I might as well link the folder so it looks a bit more obvious what I ended up doing), but I STILL was sometime getting the source index not found so I ended up doing both that and refusing the drag if that happens. Here is how I did it with everything: https://github.com/aldelaro5/Bug-Fables-Save-Editor/tree/main/BugFablesSaveEditor/BugFablesSaveEditor/Behaviors |
Beta Was this translation helpful? Give feedback.
-
Hi, @aldelaro5 , do you know how to block drag and drop if a cell of the DataGrid is being edited? This problem also happens with Avalonia.Controls.TreeDataGrid |
Beta Was this translation helpful? Give feedback.
-
Alternative solution here: |
Beta Was this translation helpful? Give feedback.
-
So if you check the Avalonia.Xaml.Behariors repos here https://github.com/AvaloniaUI/Avalonia.Xaml.Behaviors there is a dnd module and there's even samples in the repos about them working with listbox, but nothing about data grid which is what I wanted (I could have converted to listbox, but I liked the flexibility of datagrid enough to not want to change them).
So...I did one! I even got the adorner styling to work using some classes. Here's the behavior code:
As for the xaml, I just needed to put some styles in my app.xaml (but you could move these to your own file for better organisation):
And for usage, any datagrid you want to add support for dnd, you need to put
draggable
in the classes and it needs to have this underneath it:That ressource is to close the generic, it needs to be something like this and it has to be done once for every type:
I am assuming you import the namespaces under these prefixes, but you get the idea. There's not really a better way I found to get better generics handling, but it's one line per type, not too bad.
The result is REALLY nice: it works between datagrid, within a datagrid and it is smart enough to understand that dropping to the upper region of a row is different than the lower region, here it is in action:
I'm proud of getting this working so I'm sharing it :)
Beta Was this translation helpful? Give feedback.
All reactions