tui-cs/Terminal.Gui · error · InvalidOperationException
The ShadowStyle of MarginView cannot be set
Error message
The ShadowStyle of MarginView cannot be set
What it means
This error is thrown by MarginView.ShadowStyle setter because the shadow style is managed by the Margin adornment, not the MarginView. MarginView is the visual representation of a Margin adornment. To change the shadow style, developers must set it on the view's Margin (the adornment), not on the MarginView directly. The getter delegates to (Adornment as Margin)?.ShadowStyle, confirming the source of truth is the adornment.
Source
Thrown at Terminal.Gui/ViewBase/Adornment/MarginView.cs:263
else if (Adornment!.Thickness != _originalThickness)
{
_isThicknessChanging = true;
Adornment!.Thickness = new Thickness (_originalThickness.Value.Left,
_originalThickness.Value.Top,
_originalThickness.Value.Right,
_originalThickness.Value.Bottom);
_isThicknessChanging = false;
}
return style;
}
/// <inheritdoc/>
public override ShadowStyles? ShadowStyle
{
get => (Adornment as Margin)?.ShadowStyle ?? null;
set => throw new InvalidOperationException ("The ShadowStyle of MarginView cannot be set");
}
/// <summary>
/// Gets or sets the size of the shadow effect.
/// </summary>
public Size ShadowSize
{
get;
set
{
if (field == value)
{
return;
}
if (TryValidateShadowSize (field, value, out Size result))
{
field = value;View on GitHub (pinned to 2e47b11478)
Solutions
- Set the shadow style on the Margin adornment, not the MarginView: use view.Margin.ShadowStyle = ShadowStyles.* (the adornment's property, not the view's).
- Do not access or configure adornment views directly; use the adornment API on the parent view.
- In generic code, skip MarginView when setting ShadowStyle.
- Check documentation: Margin is the adornment (configurable), MarginView is its renderer (read-only).
Example fix
// before -- setting ShadowStyle on MarginView throws MarginView marginView = (MarginView)view.GetAdornments().First(a => a is MarginView); marginView.ShadowStyle = ShadowStylesOpaque; // after -- set on the Margin adornment instead view.Margin.ShadowStyle = ShadowStyles.Opaque;
Defensive patterns
Strategy: type-guard
Validate before calling
// Set ShadowStyle on the Margin adornment, not the MarginView
if (view.Margin is Margin margin) { margin.ShadowStyle = ShadowStyles.Opaque; } Type guard
bool IsMarginAdornment(object o) => o is Margin;
Try / catch
try { marginView.ShadowStyle = style; }
catch (InvalidOperationException ex) when (ex.Message.Contains("MarginView"))
{ /* set on the adornment instead: view.Margin.ShadowStyle = style */ } Prevention
- Set ShadowStyle on view.Margin.ShadowStyle (the adornment), never on the MarginView.
- Do not configure adornment views directly -- use the adornment API on the parent.
- In generic code, skip MarginView when setting ShadowStyle.
- Margin is the configuration point; MarginView is its renderer.
When it happens
Trigger: Thrown at MarginView.cs:263 when any code attempts to set the ShadowStyle property directly on a MarginView instance. This is a common mistake when developers access view.Margin.ShadowStyle (thinking Margin returns the MarginView) or iterate over SubViews and set properties generically.
Common situations: Developers confusing the Margin adornment with the MarginView, generic property-setting test tooling, deserialization code that restores all properties, or code that accesses adornment views via GetAdornments() and tries to configure them directly.
Related errors
- The Viewport of an AdornmentView cannot be modified.
- AdornmentView can only render to their Parent or Parent's Su
- Adornment must be of type Border
- Target
- Cannot change timeout while mouse is held down. Call Stop()
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/6f26c2718b5a3cf9.
Report an issue: GitHub.