Skip to content

[BUG] NumericValidationBehavior crashes when bound to nullable numeric types (e.g., decimal?) #2879

@am-mehmood

Description

@am-mehmood

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

When attaching NumericValidationBehavior to an input element (e.g., Entry) and binding it to a nullable numeric property (decimal?, int?, etc.), the application crashes if the property is null.

Error message:

Value cannot be null.

If the property is initialized with a non-null value, everything works fine. This effectively prevents the use of nullable numeric properties in binding contexts with NumericValidationBehavior.

Expected Behavior

The behavior should allow null values when binding to nullable numeric types, without throwing an exception.

Steps To Reproduce

  1. Create a ViewModel with a nullable numeric property:

public decimal? Amount { get; set; }

  1. Bind an Entry to the property with NumericValidationBehavior:
<Entry Text="{Binding Amount}">
    <Entry.Behaviors>
        <toolkit:NumericValidationBehavior />
    </Entry.Behaviors>
</Entry>
  1. Run the app while the property is null.

Link to public reproduction project repository

n/a

Environment

- .NET MAUI CommunityToolkit:
- OS:
- .NET MAUI:

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions