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:

  • 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

Answers


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


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.


Need Your Help

unable to access $scope value in directive's controller

javascript angularjs angularjs-directive angularjs-scope

I have a simple app, but some reason I am unable to access the $scope values in the directive's controller.

Button background colour

c# asp.net visual-studio-2008

I need to change the background colour of a button using C# code (Visual Studio 2008).