Pythonnet problem getting DataGrid to work

Viewed 119

I am trying to add a DataGrid to a project using pythonnet written in python. Link to gists below:

pythonnet_datagrid.py

import os
import sys
sys.path.append(os.getcwd())

import clr
clr.AddReference('wpf\PresentationFramework') 
from System import String
from System.IO import StreamReader
from System.Threading import ApartmentState, Thread, ThreadStart
from System.Windows import Application, LogicalTreeHelper, Window
from System.Windows.Markup import XamlReader

clr.AddReference('System.Collections')
from System.Windows.Controls import *
from System.Collections.Generic import *
from System.Windows.Data import Binding

class MyData(object):
    def __init__(self, name):
        self.name = name

class MyWindow(Window):
    def __init__(self):
        stream = StreamReader('example.xaml')
        window = XamlReader.Load(stream.BaseStream)

        TestDataGrid = LogicalTreeHelper.FindLogicalNode(window, "TestDataGrid")

        items = []
        items.append(MyData("Sam"))
        items.append(MyData("Bob"))

        TestDataGrid.ItemsSource = items

        Application().Run(window)  


if __name__ == '__main__':
    thread = Thread(ThreadStart(MyWindow))
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()

example.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="Gaiaform" Height="600" Width="800">
    <Grid>
        <DataGrid Name="TestDataGrid" />
    </Grid>
</Window>

I run the code and get the error:

'list' value cannot be converted to System.Collections.IEnumerable

I have used the c# List type with the MyData object inside and it errors complaining about type(s). I am probably making a simple error somewhere but after a few days of trying other stackoverflow answers such as Cannot populate a WPF DataGrid using Python, I think I need some more experienced advice.

Thanks in advance for any help offered.

0 Answers
Related