2011-11-08 28 views
1

这里是我的OnApplyTemplate的样子:检测设计模式“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

也许有地方叫DesignerProperties另一类是与你真的要使用的一个干扰。如何:

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

是的,这工作!我检查了 - 有4个!可能的DesignerProperties! – katit

0

我认为正确的代码是,

            if (DesignerProperties.GetIsInDesignTool(this)) return; 
+0

同样的错误。它说我无法访问DesignerProperties – katit