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;
Using this converter in XAML will cause the designer to fail loading the content.
There are generally 3 ways to go about it.
- Context-based Use
- Logical Validation
- 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.
if(!values 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.
return true; //We’re in design mode!