1

I'm using Ironpython 2.7. Is it possible to dynamically generate elements of a wpf window based on data found at runtime? The program will call a database and load parameters, and I'd like to generate a TextBox (and ComboBox) for each parameter loaded (e.g. if 4 parameters are loaded, the window will show 4 TextBoxes with corresponding ComboBoxes).

Is this something I can do with a property decorator? Will I have to generate the xaml dynamically to do this? For example:

    import wpf
    from System import Windows.Window

    parameters = loadfromdatabase(args)....
    xaml_filename = "program.xaml"
    textBox_list = ""
    cnt = 0
    for param in parameters:
        textBox_list += '<TextBox x:Name={} HorizontalAlignment="Left" Height="21" Width = "52" Margin="60,{},0,0" VerticalAlignment="Top"/>\n'.format(param,60+26*cnt)
        cnt += 1
    xaml_string = "<Window \n<Grid>\n" + textBox_list + "</Grid>\n</Window>"

    with open(xaml_filename, "w") as f:
        f.write(xaml_string)

    class MyWindow(Window):
        def __init__(self):
            wpf.LoadComponent(self, xaml_filename)

    if __name__ == '__main__':
        Application().Run(MyWindow())
solvador
  • 95
  • 1
  • 10

1 Answers1

0

You would dynamically generate WPF element in IronPython the same way you would do so in C#. See WPF: How to dynamically Add Controls in dynamically created WPF Window for how to do it in C#.

Community
  • 1
  • 1
tchau.dev
  • 903
  • 1
  • 11
  • 30
  • The method I suggested worked as well. Do all of the references in the C# example you mentioned translate directly to IronPython? – solvador Mar 11 '16 at 20:37
  • Yes, I believe so. Just write as you normally would in C#, except using Python syntax. – tchau.dev Mar 11 '16 at 21:04
  • Writing in XAML has its advantages, but if you must write Python code, then spare yourself the trouble of having to write XAML. – tchau.dev Mar 11 '16 at 21:11