Give WPF design mode default objects

Posted by Janko R on Stack Overflow See other posts from Stack Overflow or by Janko R
Published on 2010-04-03T01:12:20Z Indexed on 2010/04/03 1:23 UTC
Read the original article Hit count: 546

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf