lepoco/wpfui · error · InvalidOperationException

Failed to get the current `_desiredWidth`.

Error message

Failed to get the current `_desiredWidth`.

What it means

Thrown by GridViewColumn.UpdateDesiredWidth when FieldInfo.GetValue returns null for the '_desiredWidth' field on a live column instance. In practice the field is a double and should never box to null, so this guard fires only if reflection retrieved the field but the runtime returned null — e.g. the field is a nullable backing or there is an unload/teardown race. It is a defensive integrity check.

Source

Thrown at src/Wpf.Ui/Controls/GridView/GridViewColumn.cs:67

        return methodInfo;
    });

    private static MethodInfo UpdateActualWidthMethod => _updateActualWidthMethod.Value;

    /// <summary>
    /// Updates the desired width of the column to be clamped between MinWidth and MaxWidth).
    /// </summary>
    /// <remarks>
    /// Uses reflection to directly set the private `_desiredWidth` field on the `System.Windows.Controls.GridViewColumn`.
    /// </remarks>
    /// <exception cref="InvalidOperationException">
    /// Thrown if reflection fails to access the `_desiredWidth` field
    /// </exception>
    internal void UpdateDesiredWidth()
    {
        double currentWidth = (double)(
            DesiredWidthField.GetValue(this)
            ?? throw new InvalidOperationException("Failed to get the current `_desiredWidth`.")
        );
        double clampedWidth = Math.Max(MinWidth, Math.Min(currentWidth, MaxWidth));
        DesiredWidthField.SetValue(this, clampedWidth);
        _ = UpdateActualWidthMethod.Invoke(this, null);
    }

    /// <summary>
    /// Gets or sets the minimum width of the column.
    /// </summary>
    public double MinWidth
    {
        get => (double)GetValue(MinWidthProperty);
        set => SetValue(MinWidthProperty, value);
    }

    /// <summary>Identifies the <see cref="MinWidth"/> dependency property.</summary>
    public static readonly DependencyProperty MinWidthProperty = DependencyProperty.Register(
        nameof(MinWidth),

View on GitHub (pinned to ffebacd610)

Solutions

  1. Avoid calling UpdateDesiredWidth during teardown — defer or cancel width updates when the control is unloading.
  2. Reproduce on the stock runtime the library targets; if reproducible only on a newer runtime, treat as a version break (see errors 24/25).
  3. If subclassing, do not bypass the base measure pipeline that keeps _desiredWidth populated.
  4. Report upstream with the runtime version and a minimal repro if the field is genuinely null under normal use.
Defensive patterns

Strategy: try-catch

Try / catch

try { col.UpdateDesiredWidth(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to get the current"))
{
    // Column in teardown/inconsistent state; skip the clamp
    Logger.Warn($"Skipping width clamp, field value null: {ex.Message}");
}

Prevention

When it happens

Trigger: Calling UpdateDesiredWidth on a column whose underlying _desiredWidth field value is reported as null by reflection; the column is in an inconsistent state during teardown; the field exists but holds a non-double (null) due to runtime corruption.

Common situations: Column being measured/updated during window close while the GridView is being detached; very rare runtime/Reflection edge case; interaction with a custom GridViewColumn subclass.

Related errors


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/c8d2c5dd802a7bcd. Report an issue: GitHub.