WPF: UserControl constructor executes when opening in designer

M

moondaddy

I have a user control which executes code in the class constructor. The
problem is that when I open the control in the VS 2008 designer this code
executes and causes exceptions because the app isn't actually running. How
can I determine if the class is being initialized in the designer so I can
exit the constructor after the call to the InitializeComponent() method?

Thanks.
 
J

\Ji Zhou [MSFT]\

Hello George,

We can call the DesignerProperties.GetIsInDesignMode() method to judge
whether the codes are executed in the designer or in the real application.

See the following codes,

public UserControl1()
{
InitializeComponent();

if (!DesignerProperties.GetIsInDesignMode(this))
{
MessageBox.Show("test2");
}
}

Only when we run our WPF application, the message box pops up. Please let
me know if this addresses your issue. If you have any other questions or
concerns, please feel free to update this thread and I will try my best to
provide future assistance.

Have a nice day George!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top