dotnet/wpf · error · ArgumentException

SR.DataObject_DataFormatNotPresentOnDataObject

Error message

SR.DataObject_DataFormatNotPresentOnDataObject

What it means

The DataObjectPastingEventArgs constructor throws ArgumentException when the supplied formatToApply is not present on the supplied dataObject, as verified by dataObject.GetDataPresent(formatToApply). The args object's contract is that FormatToApply names a format the DataObject actually contains; otherwise consumers of the Pasting event would try to read data that does not exist.

Solutions

  1. Add the data under that format: dataObject.SetData(formatToApply, value) before constructing args
  2. Choose a format from dataObject.GetFormats() that actually exists
  3. Verify the exact format string with dataObject.GetDataPresent(formatToApply) before constructing

Example fix

// before
var args = new DataObjectPastingEventArgs(dataObject, false, DataFormats.Html);
// after
if (dataObject.GetDataPresent(DataFormats.Html))
{
    var args = new DataObjectPastingEventArgs(dataObject, false, DataFormats.Html);
}
Defensive patterns

Strategy: validation

Validate before calling

if (formatToApply == null || !dataObject.GetDataPresent(formatToApply)) throw new ArgumentException($"Format '{formatToApply}' is not present on the data object.", nameof(formatToApply));

Type guard

bool HasFormat(IDataObject data, string format) => data.GetDataPresent(format);

Try / catch

try { var args = new DataObjectPastingEventArgs(dataObject, false, format); }
catch (ArgumentException) { format = dataObject.GetFormats().FirstOrDefault(); }

Prevention

When it happens

Trigger: new DataObjectPastingEventArgs(dataObject, isInDragDrop, formatToApply) where dataObject.GetDataPresent(formatToApply) returns false — e.g. the format was set with autoConvert formats excluded, or the format name is misspelled/differs in case.

Common situations: Passing a DataFormats constant the clipboard object never carried (e.g. Bitmap when only Text exists); building a DataObject in code and forgetting AddData for the named format; format auto-conversion differences between .NET versions.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        /// 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
        //
        //------------------------------------------------------

        #region Public Properties

        /// <summary>

View on GitHub (pinned to 81131a70a4)