Point to different xaml template

I’m fiddling with makes things better as I am testing things I find myself altering the core of my pyrevit installation.

# forms > __init__.py 
xaml_source = 'SelectFromList.xaml'

How to point to a different xaml? Not using the forms.selectfromlist but create one in my script itself instead?

not sure I answer exactly your question, I lack a bit of context:

GUI = MyWindow("your_custom_xaml.xaml") 
GUI.ShowDialog()

My bad,

In a script we can call
mylist = forms.SelectFromList(list, etcetc)

I’ve changed the xaml file the class SelectFromList loads. So xaml_source is still ‘SelectFromList.xaml’ but I’ve editted the original (%appdata%/pyRevit-Master etcetc).

I’m not the greatest fan changing originals (reinstall = everything gone).

So I’d rather have that custom SelectFromList.xaml in my .extension / lib folder for example. Cleaner and better to manage.

this doesn’t work, but you get the idea. I need to point to my custom xaml somehow.
mylist = forms.SelectFromList(customxaml, list, etcetc)

Unfortunately, while the base class WPFWindow lets (well, needs) you to specify the xaml file in the constructor, the classes that inherit from TemplateUserInputWindow, such as SelectFromList, have this parameter hardcoded in them as a class (not instance) attribute.

To workaround this, you can create a class that inherits from SelectFromList and override the xaml that it loads, then use your class when you need it.

from pyrevit.forms import SelectFormList

class MyListSelector(SelectFormList):
     xaml_source = "my_custom_window.xaml"
1 Like

Oh! I didn’t know you can overwrite class variables like that.

The xaml isn’t selected for me though. Since XAML_FILES_DIR is hardcoded too in init I assume my alternative xaml file should be in the same directory as the current SelectFromList.xaml.

So for now I put in pyrevit > forms my file SelectFromList_Faster.xaml

Xaml_files_dir can’t be overwritten as it is not in a class right?

Oh, you’re right, this is definitely a design limitation that we should solve in the pyrevit library.

As of now, you would have to override the __init__ method (and at this point, you can bypass the xaml_source attribute)

I’ve added a little script that check if file exists if not: copy.

Quick n dirty way. Works just fine for time being…

appdata_path = os.getenv('APPDATA')  # Get %APPDATA% path
target_file = os.path.join(appdata_path, "pyRevit-Master", "pyrevitlib", "pyrevit", "forms", "SelectFromList_Faster.xaml")

# Source file location
source_file = "T:/dir/dir/my.extension/lib/pyrevit_overrides/SelectFromList_Faster.xaml"

# Check if the file exists, if not, copy it
if not os.path.exists(target_file):    
    shutil.copy2(source_file, target_file)
    print "faster xaml copied successfully."

Next step is writing a post about my Faster xaml. :wink:

Do you think your version could be useful to everybody? If so, just make a PR and it will be available in the next pyrevit version! :wink: