IronPython WPF Load New Window

ironpython, python, user-interface, windows, wpf

Solution

If you want to show two window in the same time you should use `Show` (msdn) or `ShowDialog` (msdn) method.

Example:

File "AboutWindow.xaml":

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AboutWindow" Height="300" Width="300">
    <Grid>
        <TextBlock Text="AboutWindow" />
    </Grid>
</Window>

File "AboutWindow.py":

import wpf

from System.Windows import Window

class AboutWindow(Window):
    def __init__(selfAbout):        
        wpf.LoadComponent(selfAbout, 'AboutWindow.xaml')

File "IronPythonWPF.xaml":

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="IronPythonWPF" Height="300" Width="300">
    <StackPanel>
        <Menu>
            <MenuItem Header="About" Click="MenuItem_Click" />
        </Menu>
        <TextBlock Text="MainWindow" />
    </StackPanel>
</Window> 

File "IronPythonWPF.py":

import wpf

from System.Windows import Application, Window
from AboutWindow import *

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'IronPythonWPF.xaml')

    def MenuItem_Click(self, sender, e):   
        form = AboutWindow()
        form.Show()        

if __name__ == '__main__':
    Application().Run(MyWindow())

Problem

Bear with me, I'm new to GUI Programming, IronPython, WPF and .NET. However, I am fairly familiar with Python. I have browsed many online tutorials, but none seem to address the exact issue I'm facing. (Perhaps it's trivial? But it is not trivial to someone like me!) Issue: I'd like to know how to launch a new WPF window (XAML) as a new window from my Windows.System.Application. Basically, I want to launch an "About" dialog from the help menu of my app. I know this can be achieved by using System.Windows.Forms.Form, but in the long run I want to be able to load more complex window arrangements by using XAML markup. Currently, when I click the About Menu (mnuAboutClick) this does load the XAML window, but in the process replaces/destroys the original Main Window (WpfMainWindow). I want both windows to remain open. EDIT: Alternatively, is there a way to load the xaml into the System.Windows.Forms.Form? This would be suitable for my needs. Here's an example of my code: ``` import wpf from System.Windows import Application, Window class MyWindow(Window): def __init__(self): wpf.LoadComponent(self, 'WpfMainWindow.xaml') def mnuAboutClick(self, sender, e): print 'About Menu Click' wpf.LoadComponent(self, 'WpfAboutWindow.xaml') # How to launch "About Dialog", This works, but destroys "WpfMainWindow"! if __name__ == '__main__': Application().Run(MyWindow()) ```

Original source