Search code examples
c#wpfcustom-controlsdesignerdesignmode

Possible issues using DesignerProperties.GetIsInDesignMode(...)?


I'm creating a WPF custom control and ran into a situation. A lot of the code does not execute until the control is initialized: if (this.IsInitialized) { ... } However, this is causing an issue with my designer because it is never initialized.

I'm just wondering if using the DesignerProperties.GetIsInDesignMode() in a custom control is normal to use, and if so, should I be wary of any pitfalls? I ask this because it just seems "dirty" to have designer-specific checking/code in a custom control.

I guess a good measuring stick would be to know if Microsoft uses Designer-specific code (not attributes) in any of their controls code?


Solution

  • This is one of the main reason this method exists in the first place.

    While I agree that it feels "dirty" to put design-specific checking in your code logic, sometimes it's the most pragmatic approach. Personally, I feel that making a control work nicely in design mode is part of the necessary implementation and function of a custom control, in which case having code specifically to handle that case is not necessarily bad.

    A lot of the code does not execute until the control is initialized:

    I would suggest looking closely at this code, however. When creating a custom control in WPF, it's often better to have code that runs based on the data to which its bound, not whether or not code has been initialized. If you run your code based off the bound data or properties, it shouldn't matter whether you're running in a designer or executing the application.