I've always enjoyed GUI development using XAML-based frameworks such as WPF, Xamarin.Forms, etc. In Python I've used PyQT and tkinter, but I really don't enjoy using either of them. I recently found out about Python.NET (http://pythonnet.github.io/), and I've been tinkering with building WPF/XAML-based apps using Python.
I have had some success, but I've also hit a lot of roadblocks. One roadblock I am trying overcome right now has to do with databinding, specifically using a DataTemplate that I've defined in the XAML. I am trying to populate a ListBox, and I've defined a DataTemplate for the items of the ListBox. Here is my XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RePlay Database Builder: Select a database"
Height="600"
Width="800">
<Grid>
<ListBox Margin="20,10" Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Name: " FontWeight="Bold" HorizontalAlignment="Left" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=MyName}" HorizontalAlignment="Left" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Path: " FontWeight="Bold" HorizontalAlignment="Left" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=MyPath" HorizontalAlignment="Left" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Now, I'd like to populate that listbox and have everything "link up" properly. Here's the current code I am attempting to use, but it's (obviously) not working:
import clr
clr.AddReference("wpf\PresentationFramework")
clr.AddReference("System.Collections")
from System.IO import *
from System.Windows.Markup import XamlReader
from System.Windows import *
from System.Threading import Thread, ThreadStart, ApartmentState
from System.Windows.Controls import *
from System.Collections.Generic import List
import System
class TestObject(System.Object):
def __init__(self, my_name, my_path):
self.MyName = my_name
self.MyPath = my_path
class MyWindow(Window):
def __init__(self):
try:
stream = StreamReader('MyWindow.xaml')
self.window = XamlReader.Load(stream.BaseStream)
self.test_list = List[TestObject]()
self.test_list.Add(TestObject("Item1", "Path1"))
self.test_list.Add(TestObject("Item2", "Path2"))
self.test_list.Add(TestObject("Item3", "Path3"))
MyListBox = LogicalTreeHelper.FindLogicalNode(self.window, "MyListBox")
MyListBox.ItemsSource = self.test_list
Application().Run(self.window)
except Exception as ex:
print(ex)
if __name__ == '__main__':
thread = Thread(ThreadStart(MyWindow))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
I was wondering if anyone could provide guidance on how to get DataTemplates working well in Python.NET. Thanks!