Skip to content

Commit c20bc9b

Browse files
committed
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
1 parent 2080fd6 commit c20bc9b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,36 @@ public IJavascriptObjectRepository JavascriptObjectRepository
490490
}
491491
}
492492

493+
494+
/// <summary>
495+
/// Indicates if one of the Ancestors of this control is sited
496+
/// and that site in DesignMode.
497+
/// </summary>
498+
// Roughly based on https://github.com/dotnet/winforms/pull/5375
499+
private bool IsParentInDesignMode(Control control)
500+
{
501+
if(control == null)
502+
{
503+
throw new ArgumentNullException(nameof(control));
504+
}
505+
506+
//Check if our Site is in DesignMode
507+
//If not then walk up the tree
508+
//Until we find a Site that is or our parent is null
509+
if(control.Site?.DesignMode ?? false)
510+
{
511+
return true;
512+
}
513+
514+
if(control.Parent == null)
515+
{
516+
return false;
517+
}
518+
519+
return IsParentInDesignMode(control.Parent);
520+
}
521+
522+
493523
/// <summary>
494524
/// Raises the <see cref="E:System.Windows.Forms.Control.HandleCreated" /> event.
495525
/// </summary>
@@ -498,6 +528,20 @@ protected override void OnHandleCreated(EventArgs e)
498528
{
499529
designMode = DesignMode;
500530

531+
//Check if our Parent is in design mode.
532+
if (!designMode)
533+
{
534+
try
535+
{
536+
designMode = IsParentInDesignMode(this);
537+
}
538+
catch (Exception)
539+
{
540+
//TODO: We should log the exception
541+
//Need to provide a wrapper around CEF Log first
542+
}
543+
}
544+
501545
if(designMode)
502546
{
503547
//For design mode only we remove our custom ApplicationExit event handler

0 commit comments

Comments
 (0)