dotnet/wpf · error · ArgumentException

SR.InvalidInkForeground

Error message

SR.InvalidInkForeground

What it means

StickyNoteControl throws ArgumentException when building its ink DrawingAttributes because the control's Foreground property is not a SolidColorBrush. Ink drawing requires a solid color to set DrawingAttributes.Color; gradients or other brushes have no single color to use.

Solutions

  1. Set StickyNoteControl.Foreground to a SolidColorBrush (e.g. new SolidColorBrush(Colors.Black)).
  2. If a dynamic brush is used, ensure it resolves to a SolidColorBrush at runtime.
  3. Wrap or convert other brush types to a solid color before assigning.

Example fix

// before
stickyNote.Foreground = new LinearGradientBrush(Colors.Black, Colors.Gray, 90);
// after
stickyNote.Foreground = new SolidColorBrush(Colors.Black);
Defensive patterns

Strategy: type-guard

Validate before calling

if ((stickyNote.Foreground as SolidColorBrush) == null)
    stickyNote.Foreground = new SolidColorBrush(Colors.Black);

Type guard

static bool HasSolidForeground(Control c) => c.Foreground is SolidColorBrush;

Try / catch

try { EnableInk(stickyNote); }
catch (ArgumentException) { stickyNote.Foreground = new SolidColorBrush(Colors.Black); EnableInk(stickyNote); }

Prevention

When it happens

Trigger: Setting Foreground on a StickyNoteControl (or its theme/style) to a non-SolidColorBrush such as LinearGradientBrush, RadialGradientBrush, ImageBrush, or DynamicResource resolving to such a brush, then entering ink mode which calls the code that reads Foreground.

Common situations: Applying a global style with gradient foreground brushes; inheriting Foreground from a parent that uses a gradient; binding Foreground to a theme resource that is not solid.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ba97d141cbcc4ab8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/StickyNote.cs:1741

        /// <summary>
        /// Update DrawingAttributes on InkCanvas
        /// </summary>
        private void UpdateInkDrawingAttributes()
        {
            if ( Content == null || Content.Type != StickyNoteType.Ink )
            {
                // Return now if there is no InkCanvas.
                return;
            }

            DrawingAttributes da = new DrawingAttributes();

            SolidColorBrush foreground = Foreground as SolidColorBrush;

            // Make sure the foreground is type of SolidColorBrush.
            if ( foreground == null )
            {
                throw new ArgumentException(SR.InvalidInkForeground);
            }

            da.StylusTip = StylusTip.Ellipse;
            da.Width = PenWidth;
            da.Height = PenWidth;
            da.Color = foreground.Color;

            // Update the DA on InkCanvas.
            ( (InkCanvas)( Content.InnerControl ) ).DefaultDrawingAttributes = da;
        }

        #endregion // Private Methods

        //-------------------------------------------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------------------------------------------

View on GitHub (pinned to 81131a70a4)