[IronPython] Referring to Python types in Xaml

Bish Bosh unittesting at gmail.com
Thu May 17 19:41:41 CEST 2007

Interesting.  My intention is not to have any C# in the projects so
cooking my own type by that method is out.   I guess people are just
not using IP as a one for one replacement for C# as I had hoped to.

In the two cases I talked about in post I guess I have workarounds.
For creating instances I can create them in Python code and add them
to the Resource database for use in the Xaml.  For templating I can
create a  typeless data template and simply refer to this when I
display the collection rather than have the system find this template
automatically by type.  No biggie really for the simple apps I'm
doing, the benefits of using Python and Xaml outweigh these wrinkles.

I wonder if the DLR is addressing these sort of typing issues?  Up to
now I'm a little unclear what those chaps are up to.

Thanks for your prompt answer.

On 5/17/07, Dino Viehland <dinov at exchange.microsoft.com> wrote:
> We don't generate a type that can be easily consumed from static languages.  Can you use an interface in the DataTemplate line, eg:
>  <DataTemplate DataType="{x:Type IFancyData}">
> If so then you could declare an interface in C# (or find an existing one which has what you want) and inherit & implement the interface in Python.  Then the interface will be the bridge between the static & dynamic worlds.
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Bish Bosh
> Sent: Thursday, May 17, 2007 8:46 AM
> To: users at lists.ironpython.com
> Subject: [IronPython] Referring to Python types in Xaml
> What is the mechanism for making types in the Python domain available
> to the WPF type system for use in a Xaml file?
> In the code below I am refering to the Python class to do some
> formatting but would have the same problem if I was trying to create
> an instance of a Python class in the Xaml file.  Any ideas? I'm pretty
> sure I'm missing something simple but fundamental.  As usual.
> In Python file I have a type:
>  class FancyData(ObservableCollection[str]):
>    pass
> The collection to be displayed I am adding as an application resource:
>   app.Resources.Add('collection', FancyData())
> So it can be bound to as expected in the Xaml:
>  <ListBox ItemsSource="{Binding Source={StaticResource collection}}"/>
> In Xaml file I would like to refer to the type to do some formatting:
>  <DataTemplate DataType="{x:Type FancyData}">
>    <StackPanel>
>       ...
> I get the error:
>  File data1.py, line 108, in Initialize
>  File , line 0, in _stub_##77
>  File data1.py, line 26, in LoadXaml
>  File , line 0, in _stub_##87
>  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 'FancyData'.
> _______________________________________________

More information about the Ironpython-users mailing list