I recently became more familiar with using the lib folder to add .py files that would get reused in several tools. What about xaml? like if I wanted to create a xaml that was a “Base_Styles.xaml” that had colors, border types, etc., how would one import that to the xaml in the pushbutton folder?
Pretty straightforward with Ironpython and not that different from any Windows dialog.
The only thing you have to watch is that it is in your search path for the script to load. I’m starting to use these more and more, so I have numerous lib locations and you need to grap the right one of course.
import wpf
from System.Windows import Application, Window
class MyDialog(Window):
def __init__(self):
# Load the XAML file
wpf.LoadComponent(self, 'dialog.xaml')
def OkClicked(self, sender, e):
# Handle button click
self.Close()
if __name__ == '__main__':
# Show the dialog
dialog = MyDialog()
dialog.ShowDialog()
i have 2 tools, toolA.xaml and toolB.xaml, i then have a “styles_manager.xaml” that i would ideally place in the lib folder and it would have the colorbrushes above. i would want to use those styles in the toolA.xaml and toolB.xaml and any other wpf’s I make.
If i then wanted to updated all the wpf’s across the board to use a different color theme, I would do this in the styles_manager.xaml.
I’m reusing a native xaml template as an item_template in customized selectfromlists like this:
import pyrevit.forms as _pf
# pyrevit.forms is a package; __file__ is its __init__.py, so dirname
# gives the forms/ directory that also holds the bundled XAML files.
xaml_path = os.path.join(
os.path.dirname(_pf.__file__), "ParameterItemStyle.xaml"
)
return forms_utils.load_ctrl_template(xaml_path)