From c20bc9b87fde3a7fbef4aea059ad01ec7afd7ac9 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Tue, 5 Apr 2022 15:37:23 +1000 Subject: [PATCH] WinForms - Improve designer support for UserControl Current DesignMode property is only true when the control is directly interacting with the designer. This checks if an ancestor is in design mode to allow support for UserControls Issue #4057 --- CefSharp.WinForms/ChromiumWebBrowser.cs | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 7bd20579f..91d6ce4c8 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -490,6 +490,36 @@ namespace CefSharp.WinForms } } + + /// + /// Indicates if one of the Ancestors of this control is sited + /// and that site in DesignMode. + /// + // Roughly based on https://github.com/dotnet/winforms/pull/5375 + private bool IsParentInDesignMode(Control control) + { + if(control == null) + { + throw new ArgumentNullException(nameof(control)); + } + + //Check if our Site is in DesignMode + //If not then walk up the tree + //Until we find a Site that is or our parent is null + if(control.Site?.DesignMode ?? false) + { + return true; + } + + if(control.Parent == null) + { + return false; + } + + return IsParentInDesignMode(control.Parent); + } + + /// /// Raises the event. /// @@ -498,6 +528,20 @@ namespace CefSharp.WinForms { designMode = DesignMode; + //Check if our Parent is in design mode. + if (!designMode) + { + try + { + designMode = IsParentInDesignMode(this); + } + catch (Exception) + { + //TODO: We should log the exception + //Need to provide a wrapper around CEF Log first + } + } + if(designMode) { //For design mode only we remove our custom ApplicationExit event handler