Importing a template.xaml?

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?

I have gotten used to doing stuff like

from multi_lines import PlaceLines

but a xaml into a xaml???

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()

and that would load resources like this to be used for any other xaml?

<SolidColorBrush x:Key="CORE_BG" Color="#1F1F1F"/>
<SolidColorBrush x:Key="CORE_PANEL" Color="#2B2B2B"/>
<SolidColorBrush x:Key="CORE_HEADER" Color="#202020"/>
<SolidColorBrush x:Key="CORE_BLUE" Color="#0080FF"/>
<SolidColorBrush x:Key="CORE_ORANGE" Color="#FF8C00"/>
<SolidColorBrush x:Key="CORE_RED" Color="#FF4646"/>
<SolidColorBrush x:Key="CORE_GREEN" Color="#00C800"/>
<SolidColorBrush x:Key="CORE_TEXT" Color="#FBFF00"/>
<SolidColorBrush x:Key="CORE_SUBTEXT" Color="#BFBFBF"/>
<SolidColorBrush x:Key="CORE_BORDER" Color="#4A4A4A"/>
<SolidColorBrush x:Key="CORE_INPUT" Color="#252525"/>

for example,

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.

Ah, Sorry, I misread the original post.
You’ll need to read in the xaml’s and parse.
XamlReader.Load()
XamlReader.Parse()
Documents over at Microsoft.

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")

from System.IO import File
from System.Windows.Markup import XamlReader
from System.Windows import Application, Window

# Load the XAML file
file_stream = File.OpenRead("MainWindow.xaml")
window = XamlReader.Load(file_stream)

# Start the application
Application().Run(window)

type or paste code here

i’ll give that a shot. thanks for the info!

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)
1 Like