{"record":{"id":"3cc270377e9b7cac","repo":"stride3d/stride","slug":"the-given-window-does-not-contain-a-contentpresenter","errorCode":null,"errorMessage":"The given window does not contain a ContentPresenter.","messagePattern":"The given window does not contain a ContentPresenter\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Commands/SystemCommands.cs","lineNumber":110,"sourceCode":"        }\n\n        private static void Close([NotNull] Window window)\n        {\n            System.Windows.SystemCommands.CloseWindow(window);\n        }\n\n        private static bool CanShowSystemMenu([NotNull] Window window)\n        {\n            return HasFlag(window, NativeHelper.WS_SYSMENU);\n        }\n\n        private static void ShowSystemMenu([NotNull] Window window)\n        {\n            // Note: as we fetch for the content presenter, this command is a bit dependent of the window control template.\n            // But both our template and the default (Aero) seems to use one so this is probably ok.\n            var presenter = window.FindVisualChildrenOfType<ContentPresenter>().FirstOrDefault(x => Equals(x.FindVisualParentOfType<Control>(), window));\n            if (presenter == null)\n                throw new InvalidOperationException(\"The given window does not contain a ContentPresenter.\");\n\n            System.Windows.SystemCommands.ShowSystemMenu(window, presenter.PointToScreen(new Point(0, 0)));\n        }\n\n        private static bool HasFlag([NotNull] Window window, int flag)\n        {\n            var hwnd = new WindowInteropHelper(window).Handle;\n            var hasFlag = (NativeHelper.GetWindowLong(hwnd, NativeHelper.GWL_STYLE) & flag) != 0;\n            return hasFlag;\n        }\n    }\n}\n","sourceCodeStart":92,"sourceCodeEnd":123,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Commands/SystemCommands.cs#L92-L123","documentation":"ShowSystemMenu locates the ContentPresenter that is a direct visual child of the given Window to compute the screen position of the system menu. If the window's control template contains no such presenter, it throws InvalidOperationException. This method is inherently dependent on the window template having a ContentPresenter.","triggerScenarios":"Calling SystemCommands.ShowSystemMenu(window) for a window whose ControlTemplate does not include a ContentPresenter directly parented to the window (custom template without one, or presenter nested deeper under other controls).","commonSituations":"Heavily customized/retemplated Window (custom chrome, third-party window shell) that renames or nests the presenter; using the method with a Window whose template hasn't been applied yet.","solutions":["Ensure the Window's ControlTemplate contains a ContentPresenter whose visual parent (nearest Control) is the Window itself.","Use the overload System.Windows.SystemCommands.ShowSystemMenu(window, point) with an explicit position instead of this helper.","Ensure OnApplyTemplate has run (window fully loaded) before calling."],"exampleFix":"// before\nShowSystemMenu(myCustomChromeWindow);\n// after\nSystem.Windows.SystemCommands.ShowSystemMenu(myCustomChromeWindow, myCustomChromeWindow.PointToScreen(new Point(0, 0)));","handlingStrategy":"validation","validationCode":"bool canShow = window.FindVisualChildrenOfType<ContentPresenter>()\n    .Any(x => Equals(x.FindVisualParentOfType<Control>(), window));\nif (canShow) ShowSystemMenu(window);","typeGuard":null,"tryCatchPattern":"try { SystemCommands.ShowSystemMenu(window); }\ncatch (InvalidOperationException) { System.Windows.SystemCommands.ShowSystemMenu(window, window.PointToScreen(new Point(0, 0))); }","preventionTips":["Keep a ContentPresenter directly under the Window in any custom window template.","Call the helper only after the window template is applied (Loaded event).","Prefer the explicit point overload for re-templated windows."],"tags":["wpf","control-template","visual-tree"],"backgroundTag":"resource-not-found","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}