dotnet/wpf · error · ArgumentException
Specified InkCanvasClipboardFormat is not valid.
Error message
Specified InkCanvasClipboardFormat is not valid.
What it means
WPF's InkCanvas ClipboardProcessor builds a map of preferred clipboard formats (XAML or Text clipboard data objects) from InkCanvasClipboardFormat values. A switch over the enum instantiates the matching data object; any value that is neither Xaml nor Text reaches the default case and throws ArgumentException (SR.InvalidClipboardFormat) naming the invalid parameter.
Solutions
- Only pass InkCanvasClipboardFormat.Xaml or InkCanvasClipboardFormat.Text
- Validate with Enum.IsDefined(typeof(InkCanvasClipboardFormat), value) before adding to the collection
- If values come from settings/config, parse defensively and fall back to the default (Xaml) format on unknown values
Example fix
// before
collection.PreferredFormat = (InkCanvasClipboardFormat)savedValue; // savedValue = 7 -> ArgumentException
// after
var format = (InkCanvasClipboardFormat)savedValue;
if (Enum.IsDefined(typeof(InkCanvasClipboardFormat), format))
{
collection.PreferredFormat = format;
}
else
{
collection.PreferredFormat = InkCanvasClipboardFormat.Xaml;
} Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(InkCanvasClipboardFormat), value))
throw new ArgumentOutOfRangeException(nameof(value));
preferredFormat = value; Type guard
static bool IsValidInkClipboardFormat(InkCanvasClipboardFormat format)
=> format == InkCanvasClipboardFormat.Xaml || format == InkCanvasClipboardFormat.Text; Try / catch
try
{
processor.PreferredFormat = value;
}
catch (ArgumentException ex)
{
// invalid InkCanvasClipboardFormat value; fall back to Xaml
processor.PreferredFormat = InkCanvasClipboardFormat.Xaml;
} Prevention
- Never cast raw ints to InkCanvasClipboardFormat without Enum.IsDefined
- Validate persisted settings before applying them as clipboard formats
- Remember the enum has exactly two members: Xaml and Text
When it happens
Trigger: Adding to ClipboardProcessor's preferred-format collection a value that is not InkCanvasClipboardFormat.Xaml or InkCanvasClipboardFormat.Text — typically an arbitrary int cast to the enum (e.g., (InkCanvasClipboardFormat)7) that skips Enum validation.
Common situations: Restoring clipboard-format preferences from config/persisted settings via raw int casts; reflection-driven property assignment with unvalidated values; assumptions that the enum has more members than it actually does.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- category
- Invalid PenTip value found in ISF stream
- InvalidEnumArgumentException: routingStrategy
- InvalidEnumArgumentException("value", (int) value…
- InvalidEnumArgumentException("value", (int) value…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d796b81675f199f2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs:283
foreach ( InkCanvasClipboardFormat format in value )
{
// If we find the duplicate format in our preferred list, we should just skip it.
if ( !preferredData.ContainsKey(format) )
{
ClipboardData clipboardData = null;
switch ( format )
{
case InkCanvasClipboardFormat.InkSerializedFormat:
clipboardData = new ISFClipboardData();
break;
case InkCanvasClipboardFormat.Xaml:
clipboardData = new XamlClipboardData();
break;
case InkCanvasClipboardFormat.Text:
clipboardData = new TextClipboardData();
break;
default:
throw new ArgumentException(SR.InvalidClipboardFormat, nameof(value));
}
preferredData.Add(format, clipboardData);
}
}
_preferredClipboardData = preferredData;
}
}
//-------------------------------------------------------------------------------
//
// Private Methods
//
//-------------------------------------------------------------------------------
#region Private MethodsView on GitHub (pinned to 81131a70a4)