[IronPython] Question Regarding WPF : DataBinding, DataTemplates and C# Extensions

Dino Viehland dinov at microsoft.com
Mon Nov 3 17:08:43 CET 2008

Declaring an interface from Python is easy:

import System
class MyComparer(System.IComparable):
    def CompareTo(self, other):
        print 'compared'
        return -1

a = MyComparer()
b = MyComparer()
c = MyComparer()
l = System.Collections.ArrayList([a,b,c])



Unfortunately I don't know enough about WPF to know why you'd get the exception you're seeing.  One thing that might help would be running with -X:ExceptionDetail so you get the full .NET stack trace as well.  That might help someone else on the list identify what's going on.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Rob Oakes
Sent: Sunday, November 02, 2008 9:01 PM
To: users at lists.ironpython.com
Subject: [IronPython] Question Regarding WPF : DataBinding, DataTemplates and C# Extensions

Dear Group,

I have a rather involved question (more accurately series of questions) regarding WPF, particularly how it relates to DataBinding and DataTemplates.  I will do my best to keep this posting to a reasonable length, but ... well ... I guess this serves somewhat as a warning ;)  If you will spare me a bit of background, I recently began trying to learn IronPython after hearing an enthusiastic endorsement from a friend and colleague.  While not a programmer or computer scientist by training (on the opposite of the spectrum, actually, my degree is in biomedical engineering); I have found myself increasing needing to work with programming tools in the past few years.  This friend pointed me toward the .Net framework, and IronPython in particular as a potential solution to several major issues we have been facing with our primary data processing tool: Matlab.

As a trial test, I therefore decided to create a small program in IronPython, namely, a Podcast Aggregator.  I purchased a Manning Early Access Program copy of Michael Foord's (excellent BTW) book, IronPython in Action and started trying to work toward my small test project.  As I also wanted to learn WPF (not to mention make my little program as pretty as possible), I decided to write the user interface for my aggregator in .xaml.

So, with that background out of the way, I arrive at a few questions.  The first of these is somewhat of a follow-up to a topic addressed in May on this very list somewhat recently (reference: http://lists.ironpython.com/pipermail/users-ironpython.com/2007-May/004912.html).  In the original post, the author (Bish Bosh) asked what the best method of referring to Python types in Xaml was.  Later in the discussion, Dino Viehland offered that it might be best to:

"Declare an interface in C# (or find an existing one which has what you want) and inherit and implement the interfaces in Python.  Then the interface will be the bridge between the static and dynamic worlds" (reference: http://lists.ironpython.com/pipermail/users-ironpython.com/2007-May/004913.html).

So, my first question: After reading all the material I have been able to find on the subject, I have not been able to locate a good tutorial or example of how this might be done.  Might it be possible to get some additional information on how this can be accomplished?  A simple example of how it can then be integrated into IronPython code would be extremely appreciated.

I have made an effort using the information available in the IronPython tutorial and a relatively simple XAML example (reference: http://blah.winsmarts.com/2007-3-WPF__The_DataTemplate,_choosing_how_your_data_will_look_like.aspx), but I have had no luck in creating a test application that will successfully load and perform as advertised.

Using the xaml, C# and mainWindow.py file below, I receive the error:

SystemError: Type reference cannot find public type named 'LocalTimes'

I am at the end of my rope and am not sure what I should try next.  Are there any ideas on how I might successfully implement a custom C# interface?


Rob Oakes

PS, Here is the source code for the simple test case I have been working with.

<< Main IPY script, called from command line with ipy TestApp.py

import clr

import os

import WindowsRSS




import LocalTimes, LocalTime

from System.IO import File

from System.Windows.Markup import XamlReader from System.ComponentModel import INotifyPropertyChanged from System.ComponentModel import PropertyChangedEventArgs from System.Windows import Application, Window

class TestApp(object):

    def __init__(self):

        xamlStream = File.OpenRead('DataBinding_20081102.xaml')

        self.Root = XamlReader.Load(xamlStream)

        self.LocalTimes = LocalTimes()

mainWin = TestApp()

app = Application()

app.Resources.Add('collection', LocalTimes())



<< DataBinding_20081102.xaml - GUI Window

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


    Title="Hello World!" Height="300" Width="300">


            <ObjectDataProvider x:Key="localTimes" ObjectType="{x:Type LocalTimes}"/>






<< LocalTimes.cs - Custom C# Class - LocalTimes

using System;

using System.Collections;

using System.Collections.Generic;

public class LocalTimes : List<LocalTime> {

      public LocalTimes() {

            this.Add(new LocalTime("New York", DateTime.Now));

            this.Add(new LocalTime("Chicago", DateTime.Now.AddHours(-1)));

            this.Add(new LocalTime("Denver", DateTime.Now.AddHours(-2)));

            this.Add(new LocalTime("Los Angeles", DateTime.Now.AddHours(-3)));



public class LocalTime {

      private string place;

      private DateTime time;

      public LocalTime(string _place, DateTime _time) {

            place = _place;

            time = _time;


      public DateTime Time {

            get { return time; }

            set { time = value; }


      public string Place {

            get { return place; }




Interpreter Output from the above files:

  File C:\Users\Rob Oakes\AppData\IronPython\IPy-Tutorial\TestApp_20081102.py, line 23, in Initialize

  File C:\Users\Rob Oakes\AppData\IronPython\IPy-Tutorial\TestApp_20081102.py, line 20, in __init__

  File , line 0, in Load##106

  File PresentationFramework, line unknown, in Load

  File PresentationFramework, line unknown, in XmlTreeBuildDefault

  File PresentationFramework, line unknown, in Parse

  File PresentationFramework, line unknown, in ParseFragment

  File PresentationFramework, line unknown, in _Parse

  File PresentationFramework, line unknown, in ReadXaml

  File PresentationFramework, line unknown, in Read

SystemError: Type reference cannot find public type named 'LocalTimes'.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081103/2669c5fd/attachment.html>

More information about the Ironpython-users mailing list