dotnet/wpf · error · InvalidOperationException

SR.CantSetInMarkup

Error message

SR.CantSetInMarkup

What it means

DialogResultConverter.ConvertFrom is intentionally unimplemented: a Window's DialogResult can only be set in code after ShowDialog, never from markup. Any attempt to convert a value into a DialogResult through the type system throws InvalidOperationException(SR.CantSetInMarkup).

Solutions

  1. Remove DialogResult from XAML and set it in code, typically in a button Click handler (this.DialogResult = true)
  2. Use the Button IsCancel/IsDefault properties for automatic OK/Cancel behavior instead of markup DialogResult
  3. For MVVM, set DialogResult via code-behind or an attached behavior rather than a markup binding

Example fix

// before
<Button Content="OK" DialogResult="True"/>
// after
<Button Content="OK" Click="OkButton_Click" IsDefault="True"/>
// code-behind
private void OkButton_Click(object sender, RoutedEventArgs e) => DialogResult = true;
Defensive patterns

Strategy: validation

Validate before calling

// detect DialogResult in XAML before applying it
if (xamlText.Contains("DialogResult="))
    throw new InvalidOperationException("DialogResult cannot be set in markup; set it in code.");

Try / catch

try { result = converter.ConvertFrom(ctx, culture, value); }
catch (InvalidOperationException ex) when (ex.Message.Contains("markup")) { /* set DialogResult in code instead */ }

Prevention

When it happens

Trigger: Setting DialogResult in XAML (e.g. <Window DialogResult="True"/> or binding/setting DialogResult on a Dialog control in markup), which routes through DialogResultConverter.ConvertFrom.

Common situations: Copying WPF sample XAML that attempts to declare DialogResult, using a designer or serializer that writes DialogResult into XAML, or data-binding DialogResult directly in markup instead of setting it in a button click handler.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DialogResultConverter.cs:61

        /// </summary>
        /// <returns>
        /// bool - Always return False
        /// </returns>
        public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) 
        {
            // We don't support ConvertTo
            return false;
        }

        /// <summary>
        /// ConvertFrom - Attempt to convert to a DialogResult from the given object. 
        /// Always throw InvalidOperation exception 
        /// </summary>
        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, 
                                           CultureInfo cultureInfo, 
                                           object source)
        {
            throw new InvalidOperationException(SR.CantSetInMarkup);
        }

        /// <summary>
        /// ConvertTo - Attempt to convert a DialogResult to the given type
        /// Always throw InvalidOperation exception 
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, 
                                         CultureInfo cultureInfo,
                                         object value,
                                         Type destinationType)
        {
            throw new InvalidOperationException(SR.CantSetInMarkup);
        }
        #endregion 
    }
}

View on GitHub (pinned to 81131a70a4)