Boolean IValueConverter for inverting a boolean value in XAML

Hi everyone,

I didn’t want to reinvent the wheel, so I thought I would ask if this already exists.

I have a need to set the IsEnabled property of WPF controls based on the value of a boolean variable (the value is also displayed in a CheckBox control), but I need to invert that boolean value. I.e. if this checkbox is checked, disable all other controls.

I quickly learned that to do this in XAML, I need an IValueConverter class. I would be happy to take a swing at adding this to the pyRevit libraries, but I wanted to ask if it already exists or if there is a simpler way to accomplish what I need to do.


You just need to set a method to handle the Click event of the CheckBox, then in that method, set the IsEnabled attribute of the controls you need. This is my approach:

class SettingForm(Window):
    def __init__(self, config):
        wpf.LoadComponent(self, script.get_bundle_file("form.xaml"))     

    def cb_createformwork_Click(self, sender, e):
        # sender is the CheckBox control. tb_thickness is the name of the other control
        self.tb_thickness.IsEnabled = sender.IsChecked

Thank you for the suggestion. I don’t think this solves the problem, though.

If the checkbox is checked, I want the other controls to be disabled. Simply changing the = to != in the code you provided doesn’t seem to work. It was my understanding that this is why a Boolean IValueConverter is needed.

I also tried this code to set IsEnabled based on either possible value of sender.IsChecked:

def cb_createformwork_Click(self, sender, e):
    if sender.IsChecked:
        self.tb_thickness.IsEnabled = False
        self.tb_thickness.IsEnabled = True

While this works, I also want to get the initial value of the checkbox from a Yes/No shared parameter of the Revit element the user has selected. I am struggling with how to set IsEnabled for all of the Window components based on the initial value of that shared parameter.

My script is intended to be a way for certain users to be able to edit and override the values of shared parameters which have UserModifiable set to No. Here is the flow of my script:

  1. User selects instance of element to edit in Revit and runs script
  2. Script reads current values of element’s shared parameters and displays them in textboxes and checkboxes
  3. If the value of a Yes/No shared parameter called auto_set is YES, all other textboxes’ and checkboxes’ IsEnabled properties should be set to False. (This auto_set parameter is used by other scripts to determine if the element should be updated/skipped. I.e. has the user overridden this element’s parameter values)
  4. The user can then uncheck the CheckBox control showing the current value (YES) of the auto_set parameter to enable editing the values of all other controls.
  5. The user can click the “Submit” button to commit a transaction of all changes to the shared parameter values and close the window OR the user can click a “Reset” button to undo any changes they have made to the checkboxes and textboxes.

I hope that makes sense… Thanks again for the help.