{"record":{"id":"deaad4d4a53f73d1","repo":"stride3d/stride","slug":"the-content-of-an-imagebutton-cannot-be-modified-by-the-user","errorCode":null,"errorMessage":"The content of an ImageButton cannot be modified by the user.","messagePattern":"The content of an ImageButton cannot be modified by the user\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.UI/Controls/ImageButton.cs","lineNumber":47,"sourceCode":"        {\n            UpdateContentImage();\n        }\n\n        private void UpdateContentImage()\n        {\n            contentImageElement.Source = ButtonImageProvider;\n        }\n\n        /// <summary>\n        /// The current content of the <see cref=\"ImageButton\"/>, that is the current image used.\n        /// </summary>\n        /// <remarks>The <see cref=\"Content\"/> of a <see cref=\"ImageButton\"/> is determined by its state (pressed/not pressed) and the value of\n        /// <see cref=\"Button.PressedImage\"/> and <see cref=\"Button.NotPressedImage\"/>. \n        /// The <see cref=\"Content\"/> cannot be set manually by the user.</remarks>\n        /// <exception cref=\"InvalidOperationException\">The user tried to modify the <see cref=\"ImageButton\"/> content.</exception>\n        public override UIElement Content\n        {\n            set { throw new InvalidOperationException(\"The content of an ImageButton cannot be modified by the user.\"); }\n        }\n\n        public override bool IsPressed\n        {\n            get { return base.IsPressed; }\n            protected set\n            {\n                if (value == IsPressed)\n                    return;\n\n                base.IsPressed = value;\n\n                UpdateContentImage();\n            }\n        }\n\n        private class ImageButtonMetadata\n        {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.UI/Controls/ImageButton.cs#L29-L65","documentation":"ImageButton derives its Content automatically from its pressed/not-pressed state, rendered from Button.PressedImage and Button.NotPressedImage. Because the content is state-driven, the library forbids assigning it manually and the Content setter unconditionally throws InvalidOperationException. Setting the visuals must be done through the image properties.","triggerScenarios":"Any code that assigns content to an ImageButton, e.g. imageButton.Content = someUIElement; or via XAML/yaml serialization setting a Content node on an ImageButton; generic UI-building code that sets Content on any Button subclass.","commonSituations":"Refactoring a Button into an ImageButton while keeping Content assignments; copy-pasted UI construction code; designers converting scene assets where a Content block remains under the ImageButton node.","solutions":["Set Button.PressedImage and Button.NotPressedImage instead of Content","If arbitrary content is needed, use a regular Button or ToggleButton rather than ImageButton","Remove the Content element from serialized UI assets for ImageButton nodes","Wrap custom visuals into images/sprites if ImageButton is required"],"exampleFix":"// before\nvar imgBtn = new ImageButton();\nimgBtn.Content = new TextBlock { Text = \"OK\" }; // throws\n// after\nvar imgBtn = new ImageButton();\nimgBtn.NotPressedImage = notPressedSprite;\nimgBtn.PressedImage = pressedSprite;","handlingStrategy":"type-guard","validationCode":"if (element is ImageButton)\n    throw new NotSupportedException(\"Set PressedImage/NotPressedImage instead of Content\");","typeGuard":"bool CanSetContent(UIElement e) => e is not ImageButton;","tryCatchPattern":"try\n{\n    button.Content = child;\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ImageButton\"))\n{\n    // fall back to setting images on the ImageButton\n}","preventionTips":["Set ImageButton visuals via PressedImage/NotPressedImage only","Use plain Button when custom content is required","Audit serialized UI assets for Content nodes under ImageButton"],"tags":["ui","imagebutton","invalid-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}