dotnet/wpf · error · ArgumentException

SR.DataObject_EmptyFormatNotAllowed

Error message

SR.DataObject_EmptyFormatNotAllowed

What it means

The DataObjectPastingEventArgs constructor throws ArgumentException when the formatToApply string is empty. A paste event args object must name a concrete data format that exists on the supplied DataObject; an empty string can never satisfy that, so the constructor rejects it up front. This guards callers constructing the args (or subclasses raising the Pasting event) from producing an unusable event payload.

Solutions

  1. Pass a real format string such as DataFormats.Text or a format present on the dataObject
  2. Check string.IsNullOrEmpty(formatToApply) before constructing the args
  3. Query dataObject.GetFormats() and use one of the returned formats

Example fix

// before
var args = new DataObjectPastingEventArgs(dataObject, false, selectedFormat ?? string.Empty);
// after
var format = string.IsNullOrEmpty(selectedFormat) ? DataFormats.Text : selectedFormat;
var args = new DataObjectPastingEventArgs(dataObject, false, format);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(formatToApply)) throw new ArgumentException("formatToApply must be a non-empty data format", nameof(formatToApply));

Type guard

bool IsValidFormat(IDataObject data, string format) => !string.IsNullOrEmpty(format) && data.GetDataPresent(format);

Try / catch

try { var args = new DataObjectPastingEventArgs(dataObject, false, format); }
catch (ArgumentException) { /* fall back to DataFormats.Text or GetFormats()[0] */ }

Prevention

When it happens

Trigger: Calling new DataObjectPastingEventArgs(dataObject, isInDragDrop, formatToApply) with formatToApply == string.Empty; also triggered by subclass constructors passing an empty format string up to the base constructor.

Common situations: Reading the format from config/clipboard metadata where the field defaults to empty string; wiring a custom paste handler before any format was selected; string concatenation producing '' when a variable is null.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataObjectPastingEventArgs.cs:55

        /// A flag indicating whether this operation is part of drag/drop.
        /// Pasting event is fired on drop.
        /// </param>
        /// <param name="formatToApply">
        /// String identifying a format an editor has choosen
        /// as a candidate for applying in Paste operation.
        /// An application can change this choice after inspecting
        /// the content of data object.
        /// </param>
        public DataObjectPastingEventArgs(IDataObject dataObject, bool isDragDrop, string formatToApply) //
            : base(System.Windows.DataObject.PastingEvent, isDragDrop)
        {
            ArgumentNullException.ThrowIfNull(dataObject);

            ArgumentNullException.ThrowIfNull(formatToApply);

            if (formatToApply == string.Empty)
            {
                throw new ArgumentException(SR.DataObject_EmptyFormatNotAllowed);
            }

            if (!dataObject.GetDataPresent(formatToApply))
            {
                throw new ArgumentException(SR.Format(SR.DataObject_DataFormatNotPresentOnDataObject, formatToApply));
            }

            _originalDataObject = dataObject;
            _dataObject = dataObject;
            _formatToApply = formatToApply;
        }

        #endregion Constructors

        //------------------------------------------------------
        //
        //  Public Properties
        //

View on GitHub (pinned to 81131a70a4)