开发者

WPF UserControl DesignMode property (Hosted on a WinForm)

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 SILVER开发者_如何学编程LIGHT
    _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...


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


Try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜