{"record":{"id":"fb6422bed30773f3","repo":"MahApps/MahApps.Metro","slug":"another-datagrid-is-already-attached-to-this-name","errorCode":null,"errorMessage":"Another DataGrid is already attached to this {nameof(DataGridColumnStylesHelperExtension)}","messagePattern":"Another DataGrid is already attached to this (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Controls/Helper/DataGridColumnStylesHelperExtension.cs","lineNumber":45,"sourceCode":"        void Detach();\n    }\n\n    [MarkupExtensionReturnType(typeof(DataGridColumnStylesHelperExtension))]\n    public class DataGridColumnStylesHelperExtension : MarkupExtension, IDataGridColumnStylesHelper\n    {\n        private WeakReference? dataGridReference;\n\n        private DataGrid? DataGrid => this.dataGridReference != null && this.dataGridReference.IsAlive ? this.dataGridReference.Target as DataGrid : null;\n\n        /// <inheritdoc />\n        public void Attach(DataGrid aDataGrid)\n        {\n            var dataGrid = aDataGrid ?? throw new ArgumentNullException(nameof(aDataGrid));\n\n            var savedDataGrid = this.DataGrid;\n            if (savedDataGrid != null && !ReferenceEquals(savedDataGrid, dataGrid))\n            {\n                throw new InvalidOperationException($\"Another DataGrid is already attached to this {nameof(DataGridColumnStylesHelperExtension)}\");\n            }\n\n            this.dataGridReference = new WeakReference(dataGrid);\n\n            dataGrid.Columns.CollectionChanged -= this.OnColumnsCollectionChanged;\n            dataGrid.Columns.CollectionChanged += this.OnColumnsCollectionChanged;\n\n            foreach (var column in dataGrid.Columns)\n            {\n                this.BindColumnStyles(column);\n            }\n        }\n\n        /// <inheritdoc />\n        public void Detach()\n        {\n            var dataGrid = this.DataGrid;\n            if (dataGrid is null)","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Controls/Helper/DataGridColumnStylesHelperExtension.cs#L27-L63","documentation":"Thrown by DataGridColumnStylesHelperExtension.Attach when a different DataGrid is already attached to the same helper instance. The helper holds a WeakReference to one DataGrid; Attach refuses to bind a second, non-identical DataGrid to prevent cross-grid style corruption. Note the string uses nameof inside an interpolation, so it renders literally as '{nameof(DataGridColumnStylesHelperExtension)}' (an unresolved-token bug) but the intent is the type name.","triggerScenarios":"Sharing one DataGridColumnStylesHelperExtension instance across multiple DataGrids; reusing a markup extension singleton; attaching the same helper to a second grid after the first was garbage-collected but the WeakReference was not cleared via Detach.","commonSituations":"Declaring the helper as a static/shared resource and referencing it from two DataGrids; a DataGrid recycled in a virtualizing container where Attach is called for a new grid while the helper still references the previous one.","solutions":["Give each DataGrid its own DataGridColumnStylesHelperExtension instance (do not share).","Call Detach() on the helper before attaching a new DataGrid.","If used as a markup extension, instantiate per-control rather than as a shared resource.","Check helper's attached DataGrid before calling Attach and Detach first."],"exampleFix":"<!-- before: shared helper reused across grids -->\n<Grid:DataGridColumnStylesHelperExtension x:Key=\"SharedHelper\" />\n<DataGrid helper:DataGridColumnStylesHelper.Extension=\"{StaticResource SharedHelper}\" />\n<DataGrid helper:DataGridColumnStylesHelper.Extension=\"{StaticResource SharedHelper}\" /> <!-- throws -->\n\n<!-- after: one instance per DataGrid -->\n<DataGrid helper:DataGridColumnStylesHelper.Extension=\"{Grid:DataGridColumnStylesHelperExtension}\" />","handlingStrategy":"validation","validationCode":"// Detach the previous DataGrid before attaching a new one, and never share instances.\npublic void SafeAttach(IDataGridColumnStylesHelper helper, DataGrid grid)\n{\n    helper.Detach();\n    helper.Attach(grid);\n}","typeGuard":null,"tryCatchPattern":"try { helper.Attach(newGrid); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Another DataGrid is already attached\"))\n{\n    helper.Detach();\n    helper.Attach(newGrid);\n}","preventionTips":["Use one DataGridColumnStylesHelperExtension instance per DataGrid; never share.","Call Detach() before attaching a different DataGrid.","As a markup extension, instantiate per-control rather than as a shared StaticResource."],"tags":["wpf","datagrid","mahapps","markup-extension","shared-state"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}