LearnRevitAPI

📊 Control Toggle Parameters in Revit with a Custom WPF Form

Feb 13, 2025

📊WPF Form - Toggle State

One of course students asked me: 'How to create a Form to control parameter toggle state with pyRevit?'

And the answer is WPF. Whenever you need to create a UI form with custom functionality, WPF is often the best choice. I know it sounds scary and it might even look intimidating, but it's not so complicated once you understand the basics.

And the best part? AI knows WPF pretty damn well, so you can get even more help from our robot friends 🤖.

But let me show you a quick solution to create a form to control toggle parameter of sheets.

P.S.
WPF is an advanced way to create UI forms. It's not for beginners, unless you are ready to learn a lot at once.


How WPF works in pyRevit (1 min guide)

Let's have a quick overview to understand how to create WPF forms with pyRevit. I will keep it very brief as I have more videos on this topic if you are interested on YouTube.

In general we need 2 scripts for a WPF form:

  • XAML Script - UI Design of our Form

  • Python Script - Regular pyRevit tool that loads XAML

XAML - Is a Extensible Application Markup Language. It's like HTML but for Window forms.

WPF - is a framework to create custom modern UI forms.

SO, we will create two files, put it in the same folder and then reference xaml inside our py file.

XAML Code

Firstly, let's look at the XAML code. As I've mentioned it's very similar to HTML structure, but it uses names from WPF framework.

To write XAML you would need to install Visual Studio IDE and set it up for WPF development.

I have a step by step video on YouTube for that.

Once you have it, here is the code we need to write. And notice that each piece of code represents an UI element in the preview window.

💡If you are a course member, you can access all code snippets in the pyRevit Code library that comes with the bundle.

So, we copy all that code and create a 📄UI.xaml file inside your pyRevit pushbutton folder.

UI Design is done.

Python Code

Alright, now the tricky part.

We need to create a class that will represent our form and connect XAML code as the base. And then we need to add methods for additional functionality.

Firstly, we need to create a python class for the form.

Let's begin with a starter code with all methods listed, and then we will go and fill them in.

We will need a few methods inside this class:

  • __init__ <- Class Constructor that will create the form itself. That's where we load XAML file, trigger populate_listbox method and show the form to a user

  • populate_listbox <- This will get all sheets in the project and add them to the ListBox

  • create_listbox_item <- This is a helper function to create WPF Control that will be added to the ListBox

  • UIe_search_text_changed <- Event handler for a search bar to filter shown items in the list.

  • UIe_toggle_visibility <- Change the parameter value when you click on CheckBoxes

  • UIe_btn_run <- Close the form with submit button

Final Code:

I will provide you the complete code and explain the basics.

  • Import .NET Classes for WPF form

  • Create a class that represents WPF Form

0️⃣✨ Define the constructor method (init)
Load XAML FIle
Trigger Populate Items Method
Show Form to the user

1️⃣➕Populate Listbox
Get all Sheets
Format the name you want to see in the list
Add listbox items using create_listbox_item method

2️⃣➕ Add List box items using behind-code.
- Create TextBlock
- Create CheckBox
- Create ListBoxItem
- Combine everything like matryoshka doll 🪆
- Add ListBoxItem to ListBox

3️⃣⚡Event Handler for SearchBox.
- Get value from search box
- Check ListBoxItems
- Hide/Show Items if it matches the search term

4️⃣⚡ Event Handler for CheckBox
- Get Sheet
- Set opposite Toggle Value

5️⃣⚡ Event Handler for Submit Button
- Close the form

P.S.

That's quite a long script, but that's because we need to control many thing in the UI form and I decided to add a search filtering as well.

WPF has a steep learning curve, but you can learn absolute basics to start creating cool custom forms for pyRevit to provide better experience and achieve more unique workflows.

If you need help, check my YouTube tutorials or you can start with my pyRevit Modern UI course here.

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 7500+ people!