MahApps/MahApps.Metro · error · InvalidOperationException

Another DataGrid is already attached to this {nameof(DataGri

Error message

Another DataGrid is already attached to this {nameof(DataGridColumnStylesHelperExtension)}

What it means

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.

Source

Thrown at src/MahApps.Metro/Controls/Helper/DataGridColumnStylesHelperExtension.cs:45

        void Detach();
    }

    [MarkupExtensionReturnType(typeof(DataGridColumnStylesHelperExtension))]
    public class DataGridColumnStylesHelperExtension : MarkupExtension, IDataGridColumnStylesHelper
    {
        private WeakReference? dataGridReference;

        private DataGrid? DataGrid => this.dataGridReference != null && this.dataGridReference.IsAlive ? this.dataGridReference.Target as DataGrid : null;

        /// <inheritdoc />
        public void Attach(DataGrid aDataGrid)
        {
            var dataGrid = aDataGrid ?? throw new ArgumentNullException(nameof(aDataGrid));

            var savedDataGrid = this.DataGrid;
            if (savedDataGrid != null && !ReferenceEquals(savedDataGrid, dataGrid))
            {
                throw new InvalidOperationException($"Another DataGrid is already attached to this {nameof(DataGridColumnStylesHelperExtension)}");
            }

            this.dataGridReference = new WeakReference(dataGrid);

            dataGrid.Columns.CollectionChanged -= this.OnColumnsCollectionChanged;
            dataGrid.Columns.CollectionChanged += this.OnColumnsCollectionChanged;

            foreach (var column in dataGrid.Columns)
            {
                this.BindColumnStyles(column);
            }
        }

        /// <inheritdoc />
        public void Detach()
        {
            var dataGrid = this.DataGrid;
            if (dataGrid is null)

View on GitHub (pinned to 72099e310b)

Solutions

  1. Give each DataGrid its own DataGridColumnStylesHelperExtension instance (do not share).
  2. Call Detach() on the helper before attaching a new DataGrid.
  3. If used as a markup extension, instantiate per-control rather than as a shared resource.
  4. Check helper's attached DataGrid before calling Attach and Detach first.

Example fix

<!-- before: shared helper reused across grids -->
<Grid:DataGridColumnStylesHelperExtension x:Key="SharedHelper" />
<DataGrid helper:DataGridColumnStylesHelper.Extension="{StaticResource SharedHelper}" />
<DataGrid helper:DataGridColumnStylesHelper.Extension="{StaticResource SharedHelper}" /> <!-- throws -->

<!-- after: one instance per DataGrid -->
<DataGrid helper:DataGridColumnStylesHelper.Extension="{Grid:DataGridColumnStylesHelperExtension}" />
Defensive patterns

Strategy: validation

Validate before calling

// Detach the previous DataGrid before attaching a new one, and never share instances.
public void SafeAttach(IDataGridColumnStylesHelper helper, DataGrid grid)
{
    helper.Detach();
    helper.Attach(grid);
}

Try / catch

try { helper.Attach(newGrid); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Another DataGrid is already attached"))
{
    helper.Detach();
    helper.Attach(newGrid);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13). Data as JSON: /api/errors/fb6422bed30773f3. Report an issue: GitHub.