katit
katit

Reputation: 17915

Detect design mode in "OnApplyTemplate" method - custom control

Here is how my OnApplyTemplate looks:

public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (DesignerProperties.IsInDesignTool) return;

            this.partTextBox = this.GetTemplateChild(PartTextBox) as TextBox;
            this.partButton = this.GetTemplateChild(PartButton) as Button;

            if (this.partTextBox == null || this.partButton == null)
            {
                throw new NullReferenceException("Template part(s) not available");
            }

            this.partTextBox.LostFocus += this.OnTextBoxLostFocus;
            this.partButton.Click += this.OnButtonClick;

            if (this.DataProvider == null)
            {
                throw new NotSupportedException("DataProvider wasn't specified");
            }

Second line where I check for IsInDesignTool gives me error saying that I can't access internal class "DesignerProperties" here.

Basically what happens is when I drag my control from toolbar into view in design it throws exception because DataProvider not specified. So, I need to disable this code for desing time.

How do I do it?

Upvotes: 1

Views: 1118

Answers (2)

RobSiklos
RobSiklos

Reputation: 8849

Maybe there's another class somewhere called DesignerProperties which is interfering with the one you really want to use. How about:

if (System.ComponentModel.DesignerProperties.IsInDesignTool) return;

Upvotes: 2

Akash Kava
Akash Kava

Reputation: 39956

I think correct code is,

            if (DesignerProperties.GetIsInDesignTool(this)) return;

Upvotes: 0

Related Questions