views:

135

answers:

1

In my application I have

    <Rectangle.Margin>
    <MultiBinding Converter="{StaticResource XYPosToThicknessConverter}">
        <Binding Path="XPos"/>
        <Binding Path="YPos"/>
    </MultiBinding>
</Rectangle.Margin>

The Data Context is set during runtime. The application works, but the design window in VS does not show a preview but System.InvalidCastException. That’s why I added a default object in the XYPosToThicknessConverter which is ugly.

class XYPosToThicknessConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
     // stupid check to give the design window its default object.
        if (!(values[0] is IConvertible))
            return new System.Windows.Thickness(3, 3, 0, 0);
    // useful code and exception throwing starts here
    // ...
    }
}

My Questions:

  • What does VS/the process that builds the design window pass to XYPosToThicknessConverter and what is way to find it out by myself.
  • How do I change my XAML code, so that the design window gets its default object and is this the best way to handle this problem?

I’m using VS2010RC with Net4.0

A: 

You'll need to make sure that the designer can get a valid copy of "XPos" and "YPos", and they are the same values as at runtime.

Chances are your DataContext is not being set in the View appropriately, so the converter gets null. If you set the DataContext to a valid object (which can be design time data), you're code should work without the defaults in the converter.

Reed Copsey