Search code examples
c#wpfdesign-time

WPF user control throws design-time exception


I have a userControl which starts a timer. It looks like the XAML designer is trying to call that code, which links to some back-end database stuff. I keep getting an unhanded exception error in the design screen.

Any ideas how I can stop the designer trying to run the code?


Solution

  • XAML designer will call the UserControl's constructor when loading in designer. In order to avoid this you can place a if condition as follows in your UserControl constructor

    if(System.ComponentModel.DesignMode) return;
    

    You can also check in this way

    if (!System.ComponenyModel.DesignProperties.GetIsInDesignMode(this))
    { // write constructor code here  }