lepoco/wpfui · critical · InvalidOperationException
The `_desiredWidth` field was not found.
Error message
The `_desiredWidth` field was not found.
What it means
Thrown lazily (on first access of DesiredWidthField) when reflection over the base System.Windows.Controls.GridViewColumn fails to locate the private instance field '_desiredWidth'. WPF UI's GridViewColumn extension clamps column width via this field; if the .NET / WPF runtime renames or removes it, the feature cannot function and the library fails fast. This is a version-compatibility break, not a usage error.
Source
Thrown at src/Wpf.Ui/Controls/GridView/GridViewColumn.cs:36
/// <ui:GridView>
/// <ui:GridViewColumn
/// MinWidth="100"
/// MaxWidth="200"
/// DisplayMemberBinding="{Binding FirstName}"
/// Header="First Name" />
/// </ui:GridView>
/// </ui:ListView.View>
/// </ui:ListView>
/// </code>
/// </example>
public class GridViewColumn : System.Windows.Controls.GridViewColumn
{
// use reflection to get the `_desiredWidth` private field.
private static readonly Lazy<FieldInfo> _desiredWidthField = new(() =>
typeof(System.Windows.Controls.GridViewColumn).GetField(
"_desiredWidth",
BindingFlags.NonPublic | BindingFlags.Instance
) ?? throw new InvalidOperationException("The `_desiredWidth` field was not found.")
);
private static FieldInfo DesiredWidthField => _desiredWidthField.Value;
// use reflection to get the `UpdateActualWidth` private method.
private static readonly Lazy<MethodInfo> _updateActualWidthMethod = new(() =>
{
MethodInfo methodInfo =
typeof(System.Windows.Controls.GridViewColumn).GetMethod(
"UpdateActualWidth",
BindingFlags.NonPublic | BindingFlags.Instance
) ?? throw new InvalidOperationException("The `UpdateActualWidth` method was not found.");
return methodInfo;
});
private static MethodInfo UpdateActualWidthMethod => _updateActualWidthMethod.Value;
/// <summary>View on GitHub (pinned to ffebacd610)
Solutions
- Pin to a .NET / WPF version known to ship the '_desiredWidth' field (check the release the library targets).
- Avoid triggering the clamp path: do not rely on ui:GridViewColumn MinWidth/MaxWidth and use the stock behavior instead.
- If you control deployment, disable AOT/trimming for the WPF assembly so reflection metadata is preserved.
- Report the break upstream so the field name / lookup can be updated for the new runtime.
Defensive patterns
Strategy: try-catch
Try / catch
try { col.UpdateDesiredWidth(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("_desiredWidth"))
{
// Runtime incompatibility: fall back to stock GridViewColumn behavior
Logger.Warn($"GridViewColumn reflection failed on this runtime: {ex.Message}");
} Prevention
- Pin to a .NET / WPF version the library officially supports.
- Do not enable trimming/AOT for assemblies using this reflection-based feature.
- Watch upstream release notes when upgrading the runtime.
When it happens
Trigger: Running the MinWidth/MaxWidth clamping feature (first call to UpdateDesiredWidth) on a .NET / WPF build where the internal field '_desiredWidth' has been renamed or removed; targeting a future .NET version that refactored GridViewColumn internals.
Common situations: Upgrading the .NET target framework (e.g. to a preview or post-.NET-8 build) where WPF internals changed; running on a trimmed/AOT-compiled binary where reflection metadata for private members was stripped; using a custom WPF fork.
Related errors
- The `UpdateActualWidth` method was not found.
- Failed to get the current `_desiredWidth`.
- Unable to get or create instance of {viewItem.TargetPageType
- Failed to create instance of the page
- The {pageType} page does not have a parameterless constructo
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/a7f4983ff0ac3a5b.
Report an issue: GitHub.