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

Rob Oakes rob.s.oakes at gmail.com
Mon Nov 3 06:00:56 CET 2008


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.h
tml).  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.h
tml).

 

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_da
ta_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?

 

Sincerely,

 

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

clr.AddReference("PresentationFramework")

clr.AddReference("PresentationCore")

clr.AddReferenceToFile("IFancyData.dll")

 

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())

app.Run(mainWin.Root)

 

>> 

 

<< DataBinding_20081102.xaml - GUI Window

 

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

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

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

      <Window.Resources>

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

      </Window.Resources>

      <Grid>

      </Grid>

</Window>

 

>> 

 

<< 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/20081102/15d888b8/attachment.html>


More information about the Ironpython-users mailing list