WPF Window (MVVM pattern) registering Application window to Parent

I came across this website and was able to create a XAML window working from within Python Node. I am still exploring the implementation so it can be useful in Dynamo environment. Perhaps something like Datashape nodes but with WPF window instead of Windows Forms.

I have attached the dyn and the xaml in here. The window opens and the command works, however when I close this window, the entire Revit closes. I am sure the close event is somewhat tied with the Revit close event. I am wondering if someone would help me understand how I can register this window as a child of Revit so when I close it, it exit without closing Revit.

[MVVM.dyn (5.1 KB) ]

Copy and paste into a text file and rename it to WpfMvvmDemo.xaml. Also in the Python script please edit the path to this file so it is found by the script.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IronPython MVVM Demo"
Width="450"
SizeToContent="Height">
<Grid Margin="15" x:Name="grid1">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" FontSize="24" Content="First Name:" />
    <Label Grid.Row="0" Grid.Column="1" FontSize="24" Content="{Binding FirstName}" />
    <Label Grid.Row="1" Grid.Column="0" FontSize="24" Content="Surname:" />
    <Label Grid.Row="1" Grid.Column="1" FontSize="24" Content="{Binding Surname}" />
    <Button Grid.Row="2" FontSize="24" Content="Change" Command="{Binding ChangeCommand}" />
</Grid>

Its because only one instance of the Application can be created per app domain, so when you close the form it calls close on the window which disposes of the Application and with it, Revit too. Instead, in your python use Show() instead:

xaml.Root.Show()

and remove the instantiation of the Application.

1 Like

Thank you!