Adding Items to DataGrid WPF using Python
<p>I am writing an application using python.net. My objective is to fill a DataGrid programmatically with data. The code I am working in is below:</p>

<pre><code>import clr
import System.Threading
System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA)

clr.AddReference(r"wpf\PresentationFramework")
clr.AddReference(r"wpf\PresentationCore")
clr.AddReference("System.Xml")

from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application, LogicalTreeHelper, MessageBox
from model import Model
from System.Windows.Media import Brushes
from random import random
from System.Windows.Controls import DataGridTextColumn, TextBox
from System.Windows.Data import Binding

xaml = """
&lt;Window
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="panel1"
       Title="harpia.ml" Height="600" Width="800" Background="#FFFBFBFB"&gt;
    &lt;Grid Margin="0,0,0,58"&gt;
        &lt;DataGrid x:Name="dataGrid" IsReadOnly="True" Background="#FFAC5C5C" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="206" Width="449"&gt;
            &lt;DataGrid.Columns&gt;

            &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
        &lt;Button x:Name="refreshBtn" Content="Button" HorizontalAlignment="Left" Margin="10,221,0,0" VerticalAlignment="Top" Width="75"/&gt;
        &lt;Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="384,221,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.033,-0.086"/&gt;
        &lt;Button x:Name="button2" Content="Button" HorizontalAlignment="Left" Height="21" Margin="10,266,0,-36" VerticalAlignment="Top" Width="75"/&gt;
        &lt;Button x:Name="button3" Content="Button" HorizontalAlignment="Left" Height="21" Margin="384,266,0,-36" VerticalAlignment="Top" Width="75"/&gt;
        &lt;TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="21" Margin="117,266,0,-36" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="236"/&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
"""

class Student(object):

    def __init__(self, name, branch, year):
            self.name = name
            self.branch = branch
            self.year = year
            print("A student object is created.")

    def print_details(self):
        """
        Prints the details of the student.
        """
        print("Name:", self.name)
        print("Branch:", self.branch)
        print("Year:", self.year)

def _button2_Click(s, e):
    textBox1 = LogicalTreeHelper.FindLogicalNode(win, 'textBox1')
    grid = LogicalTreeHelper.FindLogicalNode(win, 'dataGrid')

    col1 = DataGridTextColumn()
    col2 = DataGridTextColumn()
    col3 = DataGridTextColumn()

    grid.Columns.Add(col1)
    grid.Columns.Add(col2)
    grid.Columns.Add(col3)

    col1.Binding = Binding("name")
    col2.Binding = Binding("branch")
    col3.Binding = Binding("year")


    col1.Header = "name"
    col2.Header = "branch"
    col3.Header = "year"

    item = []

    item.append(Student("Andre", "Piratas", "1973"))
    item.append(Student("Andres", "Piratass", "1973s"))
    item.append(Student("Andre3", "Piratas3", "19733"))
    item.append(Student("Andre4", "Piratas4", "19734"))
    grid.ItemsSource = item
    textBox1.Text = str(grid.Items[0])

if __name__ == "__main__":
    xr = XmlReader.Create(StringReader(xaml))
    win = XamlReader.Load(xr)
    _button2 = LogicalTreeHelper.FindLogicalNode(win, 'button2')
    _button2.Click += _button2_Click
    Application().Run(win)
</code></pre>

<p>I was able to add the columns. However, the code is failing to add items(rows) to the DataGrid. Does anyone know how can I do this?</p>