{"record":{"id":"ca0445254dee69e2","repo":"dotnet/wpf","slug":"sr-cantsetinmarkup","errorCode":null,"errorMessage":"SR.CantSetInMarkup","messagePattern":"SR\\.CantSetInMarkup","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DialogResultConverter.cs","lineNumber":61,"sourceCode":"        /// </summary>\n        /// <returns>\n        /// bool - Always return False\n        /// </returns>\n        public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) \n        {\n            // We don't support ConvertTo\n            return false;\n        }\n\n        /// <summary>\n        /// ConvertFrom - Attempt to convert to a DialogResult from the given object. \n        /// Always throw InvalidOperation exception \n        /// </summary>\n        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, \n                                           CultureInfo cultureInfo, \n                                           object source)\n        {\n            throw new InvalidOperationException(SR.CantSetInMarkup);\n        }\n\n        /// <summary>\n        /// ConvertTo - Attempt to convert a DialogResult to the given type\n        /// Always throw InvalidOperation exception \n        /// </summary>\n        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, \n                                         CultureInfo cultureInfo,\n                                         object value,\n                                         Type destinationType)\n        {\n            throw new InvalidOperationException(SR.CantSetInMarkup);\n        }\n        #endregion \n    }\n}\n","sourceCodeStart":43,"sourceCodeEnd":78,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DialogResultConverter.cs#L43-L78","documentation":"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).","triggerScenarios":"Setting DialogResult in XAML (e.g. <Window DialogResult=\"True\"/> or binding/setting DialogResult on a Dialog control in markup), which routes through DialogResultConverter.ConvertFrom.","commonSituations":"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.","solutions":["Remove DialogResult from XAML and set it in code, typically in a button Click handler (this.DialogResult = true)","Use the Button IsCancel/IsDefault properties for automatic OK/Cancel behavior instead of markup DialogResult","For MVVM, set DialogResult via code-behind or an attached behavior rather than a markup binding"],"exampleFix":"// before\n<Button Content=\"OK\" DialogResult=\"True\"/>\n// after\n<Button Content=\"OK\" Click=\"OkButton_Click\" IsDefault=\"True\"/>\n// code-behind\nprivate void OkButton_Click(object sender, RoutedEventArgs e) => DialogResult = true;","handlingStrategy":"validation","validationCode":"// detect DialogResult in XAML before applying it\nif (xamlText.Contains(\"DialogResult=\"))\n    throw new InvalidOperationException(\"DialogResult cannot be set in markup; set it in code.\");","typeGuard":null,"tryCatchPattern":"try { result = converter.ConvertFrom(ctx, culture, value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"markup\")) { /* set DialogResult in code instead */ }","preventionTips":["Never author DialogResult in XAML; set it in code-behind after ShowDialog","Use Button.IsDefault/IsCancel for standard OK/Cancel behavior","In MVVM, close dialogs via attached behaviors or code-behind, not markup bindings"],"tags":["wpf","xaml","dialogresult"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}