WPF UserControl DesignMode 属性(托管在 WinForm 上)

发布于 2024-10-01 00:07:55 字数 1032 浏览 0 评论 0原文

我在自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2024-10-08 00:07:55

我用它来检测设计模式(我的 WPF 控件是在类库中定义的)。

    ' Exit here if in Design Mode
    If Assembly.GetEntryAssembly() Is Nothing Then Exit Sub

您可以检查 Assembly.GetEntryAssembly.FullName.ToString 如果它不是空的,并确定控件是从哪里初始化的。

当控件托管在 WinForms 中时,DesignerProperties.IsInDesignModeProperty 为我返回 null,因为 WPF 不知道那里有设计器。

史蒂夫

I used this to detect DesignMode (my WPF control is defined in a Class Library).

    ' Exit here if in Design Mode
    If Assembly.GetEntryAssembly() Is Nothing Then Exit Sub

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

月朦胧 2024-10-08 00:07:55

试试这个

    if (DesignerProperties.GetIsInDesignMode(this/*this user control*/))
    {
        // Design-mode specific functionality
    }

Try this

    if (DesignerProperties.GetIsInDesignMode(this/*this user control*/))
    {
        // Design-mode specific functionality
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文