问题
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