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线程的限制,感觉也是最稳定的一种方式,平时大可以用这种方式好了。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2021-10-14
  • 2022-12-23
  • 2021-11-12
  • 2021-06-26
猜你喜欢
  • 2021-11-30
  • 2021-10-19
  • 2021-07-29
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
相关资源
相似解决方案