Read Xaml file in dynamo by Python

Hi Everybody
I’m trying to read file xaml or create wpf form in dynamo like this video ( ) the actually thing i want is use wpf by visual studio and then read file xaml in dynamo by python instead of create form by python. But when i follow above video i got a problem when read the file xaml in dynamo like below picture

i was find the file from address link above picture

the all file i use:

Python file: (1.2 KB)

dynamo file:
TestWPF.dyn (5.0 KB)

xaml file like this picture:

Really hope have a solution for this problem, i was lost a lot of time to find solution :sob: :sob:.
Thanks all to read

Try this

from IronPython.Modules import Wpf as wpf

From pyRevit issue on github How to embed wpf with IronPython engine? · Issue #832 · eirannejad/pyRevit · GitHub


I think should import as a path if missing in Dynamo, but not sure it can resolve problem exaclly

import clt
clr.AddReferenceToFileAndPath("<the place to your reference IronPython.Wpf.dll>") 
import wpf 

it can be facing from two problem :

  • Missing Library DLLs shipped together in the time install Revit software
  • Conflict library IronPython with another third party library or extension.

Thanks for answer me

in 2022 it worked but in revit 2020, i lookup in source in DynamoForRevit i see it dont have some Dlls, and then i copy but got other problem like this:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 42, in
File “”, line 37, in init
TypeError: LoadComponent() takes exactly 3 arguments (2 given)
i will try to fix it,
Thank you for your answer

Hi @binhngo197

Normally for Revit 2020+ the ‘IronPython.Wpf’ module is integrated either into DynamoForRevit ( or into the DSIronPython package) for lower versions it is necessary to specify the paths to add like this

import clr
import sys
import System

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

from Autodesk.DesignScript.Geometry import *
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *


if sdkNumber < 2020:
    pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
    sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
    sys.path.append(pf_path + '\\IronPython 2.7\\DLLs')

except IOError:

from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application
from System.IO import StreamReader, StringReader

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

import wpf
from System.Windows import Application, Window, Thickness
from System.Windows.Controls import Button

class MyWindow(Window):
    def __init__(self):
        self.winLoad = wpf.LoadComponent(self, r"C:\Users\sneep\Downloads\gui.xaml")

    def button1_Click(self, sender, e):
        print("Button clicked!")

myWindow = MyWindow()

I was using wpf too at the beginning. However it cant run on cpython so I switched all to use window form.

1 Like

To enhance your development process, consider utilizing C# with WPF for creating add-ins. While Dynamo may seem appealing, transitioning to it would entail a significant learning curve and additional time for scripting before your end users can benefit from your work.


oh, Thank you, it is work now :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: