在“ OnApplyTemplate”中检测设计模式。方法-自定义控件 [英] Detect design mode in "OnApplyTemplate" method - custom control

查看:78
本文介绍了在“ 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的第二行错误提示我不能

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

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

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.

我该怎么做?

推荐答案

也许某个地方存在另一个称为DesignerProperties的类,它正在干扰您真正想要使用的类。怎么样:

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;

这篇关于在“ OnApplyTemplate”中检测设计模式。方法-自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆