2

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?

myermian
  • 31,823
  • 24
  • 123
  • 215

2 Answers2

1

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I am using Dependency Properties and Read-Only Dependency Properties heavily... it's just that I had to guard the OnPropertyChanged methods with IsInitialized checks to avoid issues with the order that the properties are being set. `` is different than ``. Trust me, I have never had a need to use IsInitialized except in this situation where these 2 DP intertwine and their order of calls pre-init will cause issues if not accounted for. – myermian Dec 21 '11 at 21:50
  • @m-y You can't check for null/invalid state instead, at that point? What causes this to fail at design time but work at runtime? – Reed Copsey Dec 21 '11 at 21:53
  • Basically I have a situation where I have 2 DP that can not be equal to each other. They are character values, so they can't be null as default values (instead they are '+' and '-'). Now, if I set them in XAML then there is a chance they would temporarily be equal to each other (exception thrown) pre-init... however, if I wait until after initialization I can check for their equality properly... the problem is the designer doesn't initialize a control. I guess I'll just have to utilize the Designer specific code, ah well. – myermian Dec 21 '11 at 23:48
0
public MyView()
        {
            if (DesignerProperties.GetIsInDesignMode(this))                                          
                return;

            InitializeComponent();            
        }
Pitka
  • 525
  • 5
  • 12