在“OnApplyTemplate”方法中检测设计模式 - 自定义控件

时间:2011-11-08 17:37:09

标签: c# silverlight xaml custom-controls

以下是OnApplyTemplate看起来的样子:

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");
            }

我检查IsInDesignTool的第二行给出了错误,说我无法访问内部类“DesignerProperties”。

基本上发生的事情是当我将控件从工具栏拖到设计视图中时会抛出异常,因为未指定DataProvider。因此,我需要禁用此代码以便设计时间。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

也许还有另一个名为DesignerProperties的类正在干扰你真正想要使用的类。怎么样:

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

答案 1 :(得分:0)

我认为正确的代码是,

            if (DesignerProperties.GetIsInDesignTool(this)) return;
相关问题