{"record":{"id":"0189633e85fd94ae","repo":"stride3d/stride","slug":"a-part-named-gridpartname-must-be-present-in-the","errorCode":null,"errorMessage":"A part named '{GridPartName}' must be present in the ControlTemplate, and must be of type '{typeof(Grid).FullName}'.","messagePattern":"A part named '(.+?)' must be present in the ControlTemplate, and must be of type '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Controls/CanvasView/CanvasView.cs","lineNumber":101,"sourceCode":"        {\n            var view = (CanvasView)sender;\n\n            var model = e.OldValue as IDrawingModel;\n            model?.Detach(view);\n\n            model = e.NewValue as IDrawingModel;\n            model?.Attach(view);\n\n            view.InvalidateDrawing();\n        }\n\n        public override void OnApplyTemplate()\n        {\n            base.OnApplyTemplate();\n\n            grid = GetTemplateChild(GridPartName) as Grid;\n            if (grid == null)\n                throw new InvalidOperationException($\"A part named '{GridPartName}' must be present in the ControlTemplate, and must be of type '{typeof(Grid).FullName}'.\");\n\n            var canvas = new Canvas();\n            grid.Children.Add(canvas);\n            canvas.UpdateLayout();\n            renderer = new CanvasRenderer(canvas);\n        }\n\n        protected override Size ArrangeOverride(Size arrangeBounds)\n        {\n            if (ActualWidth > 0 && ActualHeight > 0)\n            {\n                if (Interlocked.CompareExchange(ref this.isDrawingInvalidated, 0, 1) == 1)\n                {\n                    UpdateVisuals();\n                }\n            }\n\n            return base.ArrangeOverride(arrangeBounds);","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Controls/CanvasView/CanvasView.cs#L83-L119","documentation":"CanvasView.OnApplyTemplate retrieves the mandatory template part named by GridPartName and requires it to be a Grid, storing it to host the rendering canvas. If GetTemplateChild returns null or a non-Grid, the control cannot function and throws InvalidOperationException. This enforces the control/template contract for custom templates.","triggerScenarios":"Applying a ControlTemplate for CanvasView that lacks the required named Grid part, names it differently, or hosts an element of another type (e.g. Border) under that name.","commonSituations":"Restyling CanvasView with a custom template based on a generic.xaml copy that was edited; renaming the part; template not yet applied when code runs.","solutions":["Add a Grid named with GridPartName ('PART_Grid'-style key) inside the custom ControlTemplate.","Ensure the element with that name is of type Grid, not another panel.","Start from the default template in the theme dictionary and modify without removing the named part."],"exampleFix":"<!-- before: custom template missing part -->\n<ControlTemplate TargetType=\"controls:CanvasView\">\n  <Border/>\n</ControlTemplate>\n<!-- after -->\n<ControlTemplate TargetType=\"controls:CanvasView\">\n  <Grid x:Name=\"PART_Grid\"/>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"var grid = control.Template?.FindName(\"PART_Grid\", control) as Grid;\nif (grid == null) throw new InvalidOperationException(\"Custom CanvasView template lacks the required Grid part.\");","typeGuard":"bool HasGridPart(CanvasView view) => view.Template?.FindName(\"PART_Grid\", view) is Grid;","tryCatchPattern":"try { control.ApplyTemplate(); }\ncatch (InvalidOperationException ex) { log.Error(\"CanvasView template invalid\", ex); }","preventionTips":["Copy the default CanvasView template and edit without removing named parts.","Keep the Grid part name identical to GridPartName and its type exactly Grid.","Add a smoke test applying the custom template before release."],"tags":["wpf","control-template","template-parts"],"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"}