Detect design mode in “OnApplyTemplate” method - custom control

牧云@^-^@ 提交于 2019-12-11 13:54:57

问题


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?


回答1:


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;



回答2:


I think correct code is,

            if (DesignerProperties.GetIsInDesignTool(this)) return;


来源:https://stackoverflow.com/questions/8054705/detect-design-mode-in-onapplytemplate-method-custom-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!