WPF UserControl DesignMode 属性(托管在 WinForm 上)
我在自定义 WPF UserControl 上搜索 DesignMode
布尔值...如何正确实施它?
我有一个 WPF 控件托管在 WinForm 中。我发现“DesignerProperties”类在这种情况下不起作用。
我在构造函数中有一些逻辑会在设计模式中引发异常,并且想要跳过该代码,因为我没有在设计器中看到带有 UserControl 的表单。
我尝试过
private static bool? _isInDesignMode;
/// <summary>
/// Gets a value indicating whether the control is in design mode
/// (running in Blend or Visual Studio).
/// </summary>
public static bool IsInDesignModeStatic
{
get
{
if (!_isInDesignMode.HasValue)
{
#if SILVERLIGHT
_isInDesignMode = DesignerProperties.IsInDesignTool;
#else
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode
= (bool)DependencyPropertyDescriptor
.FromProperty(prop, typeof(FrameworkElement))
.Metadata.DefaultValue;
#endif
}
return _isInDesignMode.Value;
}
}
,但这不起作用:((我看到设计器异常被 IsInDesignModeStatic 代码行“阻止”...
I search DesignMode
boolean on a custom WPF UserControl... How correctly do I impelment it?
I have a WPF Control hosted in a WinForm. I saw that the "DesignerProperties" class does not work in such a case.
I have some logic in the constructor that throws exceptions in the design mode and want to skip that code, because I don't arrive to see a Form with my UserControl in the designer.
I tried
private static bool? _isInDesignMode;
/// <summary>
/// Gets a value indicating whether the control is in design mode
/// (running in Blend or Visual Studio).
/// </summary>
public static bool IsInDesignModeStatic
{
get
{
if (!_isInDesignMode.HasValue)
{
#if SILVERLIGHT
_isInDesignMode = DesignerProperties.IsInDesignTool;
#else
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode
= (bool)DependencyPropertyDescriptor
.FromProperty(prop, typeof(FrameworkElement))
.Metadata.DefaultValue;
#endif
}
return _isInDesignMode.Value;
}
}
but this does not work :(( I see designer exceptions at "blocked" with IsInDesignModeStatic code lines...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用它来检测设计模式(我的 WPF 控件是在类库中定义的)。
您可以检查 Assembly.GetEntryAssembly.FullName.ToString 如果它不是空的,并确定控件是从哪里初始化的。
当控件托管在 WinForms 中时,DesignerProperties.IsInDesignModeProperty 为我返回 null,因为 WPF 不知道那里有设计器。
史蒂夫
I used this to detect DesignMode (my WPF control is defined in a Class Library).
You may be able to check Assembly.GetEntryAssembly.FullName.ToString if it is not nothing and determine where the control is being initialized from.
The DesignerProperties.IsInDesignModeProperty was returning null for me when the control was hosted in WinForms because WPF doesn't know there is a designer there.
Steve
试试这个
Try this