Janko R
Janko R

Reputation: 137

Give WPF design mode default objects

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:

I’m using VS2010RC with Net4.0

Upvotes: 0

Views: 1133

Answers (2)

keyle
keyle

Reputation: 2837

Try put a fallback value to your binding. That's what I do to get stuff to display 'as if' in the design mode.

Something="{Binding Smthing, FallbackValue='hello world'}"

HTH

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564641

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.

Upvotes: 0

Related Questions