WPF Designer

January 15, 2009

tags: ,
no comments

There are times in which you would have code that does some runtime work that will cause the designer to fail loading the WPF content and display the error.


At such times, you might want to tweak your code to support this need.


Let’s look at the following example for a converter:

public class TemplatePickEnabledConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int id = (int)values[0];
        //Do Something..

    }
}

Using this converter in XAML will cause the designer to fail loading the content.


There are generally 3 ways to go about it.



  1. Context-based Use

  2. Logical Validation

  3. DesignMode Awareness

1) Context-based Use


The idea is to use the code dependent on the runtime environment (in this example – the converter) only when the code is basically IN the runtime environment.


There are many ways to acheive this, you can use triggers with the possibility to bound them to a static property, glue everything in runtime and what not.


2) Logical Validation


The idea is to insert some sort of validation logic.


E.g.


if (!values[0] is int) return true;


3) DesignMode Awareness


This could be identified as a specific implementation for the first way (Context-based Use)
We could rewrite our converter to behave differently when we are in design mode.


E.g.


if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
    return true; //We’re in design mode!
}

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*