WPF中的DesignMode判断

WPF中提供你一个类似WinForm的DesignMode属性的方法来判断当前是否处于设计器模式:

    bool IsInDesignMode
    {
        get { return DesignerProperties.GetIsInDesignMode(this); }
    }

对于非UI对象,要判断是否处于设计器模式,则可以这么使用:

    bool IsInDesignMode
    {
        get { return DesignerProperties.GetIsInDesignMode(new DependencyObject()); }
    }

但是,这两种方式有时会失效(具体什么情况下会失效不明),这个时候,则可以试一下如下这种方法。

    bool IsInDesignMode
    {
        get
        {
            return (bool)DesignerProperties.IsInDesignModeProperty
                        .GetMetadata(typeof(DependencyObject)).DefaultValue;
        }
    }

这种方式没有UI线程的限制,感觉也是最稳定的一种方式,平时大可以用这种方式好了。

原文地址:https://www.cnblogs.com/TianFang/p/3329232.html