# WPF form kills Dynamo and Civil 3D interface

**URL:** <https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741>\
**Category:** Civil 3D\
**Tags:** python, developers\
**Created:** [September 30, 2020, 8:50pm UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741 "2020-09-30T20:50:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdammReilly](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/adammreilly/32/95902_2.png) [@AdammReilly](https://forum.dynamobim.com/u/AdammReilly)\
**Post date:** [September 30, 2020, 8:50pm UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741/1 "2020-09-30T20:50:52Z")

</div>

I’m playing around with making some different GUI nodes with python. I have a standard python GUI that works, but I tried out using a WPF form which also works. What I’ve been having problems with is once the WPF form closes, the Dynamo window also closes and all of the UI elements in Civil 3D disappear. I end up having to close Civil 3D, which results in a crash, or ending the task to avoid the crash warnings.

Has anyone else encountered this?

---

<div class="post-metadata">

**Author:** ![AdammReilly](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/adammreilly/32/95902_2.png) [@AdammReilly](https://forum.dynamobim.com/u/AdammReilly)\
**Post date:** [September 30, 2020, 8:53pm UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741/2 "2020-09-30T20:53:00Z")

</div>

I’ve attached the sample data, if anyone wants to try it out.  
[PythonGUItest.zip.txt](https://forum.dynamobim.com/uploads/short-url/rxNM9U62BTVDTFruUC9WPd9xygF.txt) (9.8 KB)

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [October 2, 2020, 9:56pm UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741/3 "2020-10-02T21:56:03Z")

</div>

Hello @AdammReilly  
here an example of use, the Wpf and Winform methods are not quite the same

 ![testWpf (6)](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/3/13f51f4b646292522cc92167d585310b4c1a85e0.gif)

```
import sys
import clr
import wpf
clr.AddReference("System.Xml")
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
import System
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application

class HelloWorld(Window):
	def __init__ (self, xaml):
		self._xaml = xaml	
		xr = XmlReader.Create(StringReader(self._xaml))
		self.winLoad = wpf.LoadComponent(self, xr)
		self._comboBox = self.winLoad.FindName('pulldown')
		for x in range(1,10):
			self._comboBox.Items.Add("Item "+str(x))
		
	def okButtonPressed(self, sender, e):
		System.Windows.MessageBox.Show("HelloWorld")
		
	def CnlButtonPressed(self, sender, e):
		System.Windows.MessageBox.Show("are you leaving us already?")	
		self.Close()	

	def pulldown_SelectionChanged(self, sender, e):
		System.Windows.MessageBox.Show("you have selected {}".format(sender.SelectedItem.ToString()))	

xaml = """
<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="PythonGUItest" Height="300" Width="300"> 
       <Grid>
        <Button x:Name="btnOkay" Content="OK" HorizontalAlignment="Left" Margin="127,239,0,0" VerticalAlignment="Top" Width="75" Click="okButtonPressed"/>
        <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="207,239,0,0" VerticalAlignment="Top" Width="75" Click="CnlButtonPressed"/>
        <ComboBox x:Name="pulldown" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="pulldown_SelectionChanged"/>
    </Grid>
</Window> """

hello = HelloWorld(xaml)
hello.ShowDialog()

```

_Note:_  
_in terms of debugging it is much easier to use Winform rather than Wpf with IronPython_

---

<div class="post-metadata">

**Author:** ![AdammReilly](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/adammreilly/32/95902_2.png) [@AdammReilly](https://forum.dynamobim.com/u/AdammReilly)\
**Post date:** [October 2, 2020, 10:35pm UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741/4 "2020-10-02T22:35:00Z")

</div>

Thanks @c.poupin That is pretty helpful.  
In regards to your note at the bottom, I was only using WPF because I can use VS to design the form, I’m still too new to python to work out the code to build the UI there. But, I see that it’s a little simpler to stick with that and may need to find the time for it.

---

<div class="post-metadata">

**Author:** ![SeanP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seanp/32/42560_2.png) [@SeanP](https://forum.dynamobim.com/u/SeanP)\
**Post date:** [October 4, 2020, 2:42am UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741/5 "2020-10-04T02:42:11Z")

</div>

You could always design the (form) xml in VS and copy to Python as there shouldn’t be much translation.

---

<div class="post-metadata">

**Author:** ![AdammReilly](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/adammreilly/32/95902_2.png) [@AdammReilly](https://forum.dynamobim.com/u/AdammReilly)\
**Post date:** [October 5, 2020, 1:02pm UTC](https://forum.dynamobim.com/t/wpf-form-kills-dynamo-and-civil-3d-interface/55741/6 "2020-10-05T13:02:26Z")

</div>

Thanks to @c.poupin, I was able to get this code to work:

```
import sys
import clr
import wpf
clr.AddReference("System.Xml")
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
import System
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application

sheetSets = IN[0]

class SelectSheetSet(Window):
	def __init__ (self, xaml):
		self._xaml = xaml	
		xr = XmlReader.Create(StringReader(self._xaml))
		self.winLoad = wpf.LoadComponent(self, xr)
		self._comboBox = self.winLoad.FindName('pulldown')
		for x in sheetSets:
			self._comboBox.Items.Add(x.Name)
		self.output = ""
		
	def okButtonPressed(self, sender, e):
		#System.Windows.MessageBox.Show("You selected " + self._comboBox.SelectedItem)
		self.output = sheetSets[self._comboBox.SelectedIndex]
		self.Close()
		
	def CnlButtonPressed(self, sender, e):	
		self.Close()

xaml = """
<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="PythonGUItest" Height="100" Width="300">
    <Grid>
        <Button x:Name="btnOkay" Content="OK" HorizontalAlignment="Left" Margin="127,39,0,0" VerticalAlignment="Top" Width="75" Click="okButtonPressed"/>
        <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="207,39,0,0" VerticalAlignment="Top" Width="75" Click="CnlButtonPressed"/>
        <ComboBox x:Name="pulldown" HorizontalAlignment="Left" Margin="162,10,0,30" Width="120"/>
        <Label Content="Select an open Sheet Set:" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window> """

selectSet = SelectSheetSet(xaml)
selectSet.ShowDialog()
result = selectSet.output

OUT = result

```

This will let the user select an open sheet set, which is step one of my ultimate goal with this graph 😃
