views:

65

answers:

1

We've been successfully using clrtype with IronPython 2.6 and Silverlight for databinding, based on the example provided by Lukás(:

http://gui-at.blogspot.com/2009/11/inotifypropertychanged-and-databinding.html

We create the binding when we create the datagrid columns programatically. Because we are using IronPython some of the static databinding techniques you would normally use with C# don't work.

I've been trying (and failing) to get a column in the grid show different colors based on databinding.

I've got the colored bubble showing in the grid, but can't get databinding to the color to work. First the basics.

This is the xaml for the bubble with a fixed color:

<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'
 xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'&gt;
<Ellipse Stroke="#FF222222" Height="15" Width="15">
<Ellipse.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop x:Name="bubbleColor" Offset="0.694"
            Color="#FF00FF40" />
<GradientStop Color="#FFE6E6E6"/>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>

I can add a column based on this template very simply. The loadXaml function is a thin wrapper around XamlReader.Load:

     from com_modules.loadxaml import loadXaml
     from System.Windows.Controls import DataGridTemplateColumn
     column = DataGridTemplateColumn()
     column.CellTemplate = loadXaml('templatecolumn')
     column.Header = 'Bubble'

     grid.Columns.Add(column)

If I try to naively specify a binding in the xaml then I get a PARSER_BAD_PROPERTY_VALUE when I attempt to load the xaml (so no hope of setting up the binding after load):

<GradientStop x:Name="bubbleColor" Offset="0.694" Color="{Binding color}" />

One approach I tried was to create a ValueConverter. Here is the skeleton of the class I created:

from System import Type
from System.Globalization import CultureInfo
from System.Windows.Data import IValueConverter

class ColorConverter(IValueConverter):
   _clrnamespace = "Converters"
   __metaclass__ = clrtype.ClrClass

   @clrtype.accepts(object, Type, object, CultureInfo)
   @clrtype.returns(object)
   def Convert(self, value, targetType, parameter, culture):
     pass

   @clrtype.accepts(object, Type, object, CultureInfo)
   @clrtype.returns(object)
   def ConvertBack(self, value, targetType, parameter, culture):
     pass

As there is a _clrnamespace specified I thought I might then be able to use this converter in xaml. Trying to reference the ColorConverter class in the Converters namespace in a resources dictionary again causes blow ups when loading the xaml.

Setting this up programatically would be ideal. Anyone got any ideas?

+1  A: 

I don't know anything about IronPython, but I know that you cannot bind to a Color in Silverlight, regardless of the language used. This has caused me many grievances. In Silverlight 3 you can only bind properties on a FrameworkElement, and since GradientStop is a DependencyObject, it will not work. The good news is that Silverlight 4 will get rid of that limitation and allow you to bind properties on DependencyObject. I haven't tried it though, so I cannot say for sure. More info here:
http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#dobind

At the moment, what you could do is to bind the Fill property on the Ellipse instead. But then you will have to create the entire LinearGradientBrush in your converter code, so it's a bit complicated.

Henrik Söderlund
Ok, and it turns out that because there isn't a real (on-disk) assembly behind classes created with clrtype the Type.GetType call that xaml loading does to find the value converter fails. Seems like I'm a bit doomed to failure with this approach.
fuzzyman