{"record":{"id":"5d6deed98998ab70","repo":"stride3d/stride","slug":"can-not-convert-value-to-the-required-type","errorCode":null,"errorMessage":"Can not convert value to the required type","messagePattern":"Can not convert value to the required type","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Quantum/ViewModels/NodeViewModel.cs","lineNumber":588,"sourceCode":"        if (!IsDestroyed)\n        {\n            Refresh();\n        }\n        UpdateViewModelProperties();\n        OnPropertyChanged(nameof(VisibleChildrenCount));\n\n        OnPropertyChanged(nameof(NodeValue));\n        owner.NotifyNodeChanged(this);\n\n        valueChanging = false;\n    }\n\n    private object? ConvertValue(object value)\n    {\n        if (value == null)\n            return null;\n        if (!TypeConverterHelper.TryConvert(value, Type, out var convertedValue))\n            throw new InvalidCastException(\"Can not convert value to the required type\");\n        return convertedValue;\n    }\n\n    private void AddChild(NodeViewModel child) => ChangeAndNotify(() => { child.Parent = this; ((ICollection<NodeViewModel>?)initializingChildren ?? children).Add(child); }, $\"{GraphViewModel.HasChildPrefix}{child.Name}\", child.Name);\n\n    private void RemoveChild(NodeViewModel child) => ChangeAndNotify(() => { child.Parent = null; ((ICollection<NodeViewModel>?)initializingChildren ?? children).Remove(child); }, $\"{GraphViewModel.HasChildPrefix}{child.Name}\", child.Name);\n\n    private void AddCommand(NodePresenterCommandWrapper command) => ChangeAndNotify(() => commands.Add(command), $\"{GraphViewModel.HasCommandPrefix}{command.Name}\", command.Name);\n\n    private void RemoveCommand(NodePresenterCommandWrapper command) => ChangeAndNotify(() => commands.Remove(command), $\"{GraphViewModel.HasCommandPrefix}{command.Name}\", command.Name);\n\n    private void AddAssociatedData(string key, object value) => ChangeAndNotify(() => associatedData.Add(key, value), $\"{GraphViewModel.HasAssociatedDataPrefix}{key}\", key);\n\n    private void RemoveAssociatedData(string key) => ChangeAndNotify(() => associatedData.Remove(key), $\"{GraphViewModel.HasAssociatedDataPrefix}{key}\", key);\n\n    private void ChangeAndNotify(Action changeAction, params string[] propertyNames)\n    {\n        ArgumentNullException.ThrowIfNull(changeAction);","sourceCodeStart":570,"sourceCodeEnd":606,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Quantum/ViewModels/NodeViewModel.cs#L570-L606","documentation":"NodeViewModel.ConvertValue throws InvalidCastException when TypeConverterHelper.TryConvert cannot convert the given value to the node view model's Type. It fires during value assignment when the incoming object is non-null and not convertible. This guards the view model against incompatible values.","triggerScenarios":"Setting a NodeViewModel's value with an object whose type cannot be converted to the node's Type via TypeConverterHelper — e.g. assigning a string where a struct/color is expected and no type converter is registered.","commonSituations":"Property grid edits with free-text input for non-convertible types; binding a view model property to a source of mismatched type; custom types lacking registered type converters.","solutions":["Ensure the value's type matches the node's Type, or register a TypeConverter for it.","Pre-convert with TypeConverterHelper.TryConvert and handle failure before assignment.","Pass null instead of a non-convertible object if null is acceptable for the node.","Check for a model/view-model type mismatch after refactoring."],"exampleFix":"// before\nnodeViewModel.Value = rawString; // InvalidCastException if not convertible\n// after\nif (TypeConverterHelper.TryConvert(rawString, nodeViewModel.Type, out var converted))\n    nodeViewModel.Value = converted;\nelse\n    log.Warn($\"Cannot convert {rawString} to {nodeViewModel.Type}\");","handlingStrategy":"type-guard","validationCode":"if (value != null && !TypeConverterHelper.TryConvert(value, targetType, out _))\n    throw new InvalidOperationException($\"{value.GetType()} is not convertible to {targetType}\");","typeGuard":"static bool IsConvertible(object value, Type targetType) =>\n    value == null || TypeConverterHelper.TryConvert(value, targetType, out _);","tryCatchPattern":"try\n{\n    nodeViewModel.Value = value;\n}\ncatch (InvalidCastException ex)\n{\n    logger.Warn(ex, \"Value assignment rejected: type not convertible\");\n}","preventionTips":["Pre-convert with TypeConverterHelper.TryConvert before assignment.","Register TypeConverters for custom types shown in property grids.","Match binding source types to node types to avoid silent mismatches."],"tags":["dotnet","type-conversion","viewmodel","invalid-cast"],"backgroundTag":"type-mismatch","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"}