Continuing my journey into trying to understand xaml, I’ve been playing around with trying to implement Revit’s dragdrop method. Using this series of posts as a reference, I’ve tried implementing a super simple version but it isn’t working. I think it must be because the x:Class cannot be implemented in Ironpython as it can in C# (as far as I’ve been able to understand). Is there a way to use dragdrop with xaml in pyrevit? I tried both form.Show() (dragging crashes revit) and form.ShowDialog() (dragging shows the circle with a slash cursor).
Here is the code. As a test, I was able to get it working with winforms just fine, but I really want to be able to use xaml so I can implement it in my dockable panels.
Xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="title"
Title="DragDropTest" Width="300" Height="300" ResizeMode="CanResize"
WindowStartupLocation="CenterScreen">
<StackPanel Margin="10">
<Label Content="DRAG THIS TO TEST"
MouseMove = "label_MouseMove"
Tag="C:\Users\kfaulkner\Downloads\MicrosoftTeams-image (2).png"/>
</StackPanel>
</Window>
Python code-behind:
import Autodesk.Revit.UI as UI
import clr
clr.AddReference('IronPython.Wpf')
import wpf
from System import Windows
import os
lib = os.path.dirname(__file__)
xamlfile = os.path.join(lib, 'DragDrop.xaml')
class DropHandler(UI.IDropHandler):
def Execute(self, document, data):
print str(data)
class DragDropTest(Windows.Window):
def __init__(self):
wpf.LoadComponent(self, xamlfile)
def label_MouseMove(self, sender, args):
dropHandler = DropHandler()
UI.UIApplication.DoDragDrop(sender.Tag, dropHandler)
dragdropform = DragDropTest()
dragdropform.ShowDialog()
Winforms version that works:
import Autodesk.Revit.DB as DB
import Autodesk.Revit.UI as UI
import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
import System
class DropHandler(UI.IDropHandler):
def Execute(self, document, data):
print str(data)
class DragDropWinform(System.Windows.Forms.Form):
def __init__(self):
self.InitializeComponent()
def InitializeComponent(self):
self._label1 = System.Windows.Forms.Label()
self.SuspendLayout()
self._label1.Location = System.Drawing.Point(13, 24)
self._label1.Name = "label1"
self._label1.Size = System.Drawing.Size(259, 23)
self._label1.TabIndex = 0
self._label1.Tag = "\"C:\\Users\\kfaulkner\\Downloads\\MicrosoftTeams-image (2).png\""
self._label1.Text = "DRAG THIS TO TEST"
self._label1.MouseMove += self.Label1MouseMove
self.ClientSize = System.Drawing.Size(284, 65)
self.Controls.Add(self._label1)
self.Name = "MainForm"
self.Text = "DragDropTest"
self.ResumeLayout(False)
def Label1MouseMove(self, sender, e):
dropHandler = DropHandler()
UI.UIApplication.DoDragDrop(sender.Tag, dropHandler)
dragdropform = DragDropWinform()
System.Windows.Forms.Application.Run(dragdropform)