Two way binding to wpf not working

Not able to get it working, banging my head for several hours. (Bleeding.) Any advice would be appreciated :slight_smile:

The relevant xml line is:
<TextBox x:Name="bounded_var" Text="{Binding PythonVariable, Mode=TwoWay}"/>

The script.py:

class MyWindow(Windows.Window):
    def __init__(self):
        wpf.LoadComponent(self, xamlfile)
   
    def dev(self, sender, args):
        print(self,  self.PythonVariable)

MyWindow().ShowDialog()

yet triggering the dev function to run (from a button click) triggers the following trace:

...\script.py", line 47, in dev
AttributeError: 'MyWindow' object has no attribute 'PythonVariable'

Any Advice?

First advice: use triple backticks to enclose your code as stated in the quick start topic :wink: I’ve done it for you.

The error clearly states that the class doesn’t have the attribute PythonVariable, you need to initialize it in the __init__ method.

Thank you sir.

Your advice did get rid of the error, yet neither binding has occurred :cold_sweat:.

  1. The value that is set in the constructor does not appear in the dialog text box.
  2. Changing the value of the dialog text box does not impact the value printed in the dev function, it still shows the value set in the constructor.

The revised code:

class MyWindow(Windows.Window):
    def __init__(self):
        wpf.LoadComponent(self, xamlfile)
        self.PythonVariable = "Bluh"
   
    def dev(self, sender, args):
        print(self,  self.PythonVariable)

MyWindow().ShowDialog()

Of course it doesn’t work, you need to implement reactivity for the properties you want to bind.

Have a look at pyrevit notion example

Pyrevit forms module already has the Reactive class in it, so you can use it place of the Windows.Window, and has a @reactive decorator to use in place of @property to enable bindings.

Can I ask you if you did search for your problem before posting?
Did you take a look to the quick start pinned topic to see if there were some useful resources?
Did you struggle in finding useful info? where and how?
I ask this to identify the flaws in the current “pyrevit knowledge system” and try to fix them.
Thanks and happy coding!