在Winforms中,您可以这样说
if ( DesignMode )
{
// Do something that only happens on Design mode
}在WPF中有这样的东西吗?
发布于 2009-01-08 21:35:12
确实存在
示例:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
public class MyUserControl : UserControl
{
public MyUserControl()
{
if (DesignerProperties.GetIsInDesignMode(this))
{
// Design-mode specific functionality
}
}
}发布于 2010-01-05 01:06:36
在某些情况下,我需要知道对非UI类的调用是否是由设计器发起的(比如我是否从XAML创建了一个DataContext类)。那么from this MSDN article的方法是有帮助的:
// Check for design mode.
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
//in Design mode
}发布于 2010-11-09 03:50:16
对于WinForms中承载的任何WPF控件,DesignerProperties.GetIsInDesignMode(this)都不起作用。
因此,我创建了a bug in Microsoft Connect并添加了一个解决方法:
public static bool IsInDesignMode()
{
if ( System.Reflection.Assembly.GetExecutingAssembly().Location.Contains( "VisualStudio" ) )
{
return true;
}
return false;
}https://stackoverflow.com/questions/425760
复制相似问题