MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentException

Password cannot be empty

Error message

Password cannot be empty

What it means

`FieldsViewModel.Password1Validated` (in the MainDemo fields sample) throws `ArgumentException` from its setter if the new value is null or empty. It exists to demonstrate explicit validation on a password-style field backed by `SetProperty`.

Source

Thrown at src/MainDemo.Wpf/Domain/FieldsViewModel.cs:40

    [ObservableProperty]
    private string? _text1;

    [ObservableProperty]
    private string? _text2;

    [ObservableProperty]
    private string? _password1 = string.Empty;

    [ObservableProperty]
    private string? _password2 = "pre-filled";

    public string? Password1Validated
    {
        get => _password1Validated;
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("Password cannot be empty");
            SetProperty(ref _password1Validated, value);
        }
    }

    public string? Password2Validated
    {
        get => _password2Validated;
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("Password cannot be empty");
            SetProperty(ref _password2Validated, value);
        }
    }

    public FieldsTestObject TestObject => new() { Name = "Mr. Test" };

    [ObservableProperty]

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Never bind/copy the empty default of `_password1` into `Password1Validated`; only set it once the user has entered a non-empty value.
  2. Use `INotifyDataErrorInfo` / `ValidationRule` instead of throwing from the setter so an empty box is flagged rather than crashing the binding.
  3. If empty is acceptable, change the guard to treat whitespace-only or only allow the throw on explicit submission.
  4. Guard the assignment site: `if (!string.IsNullOrEmpty(newValue)) vm.Password1Validated = newValue;`.

Example fix

// before
public string? Password1Validated
{
    get => _password1Validated;
    set
    {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentException("Password cannot be empty");
        SetProperty(ref _password1Validated, value);
    }
}

// after
public string? Password1Validated
{
    get => _password1Validated;
    set
    {
        if (string.IsNullOrEmpty(value))
        {
            AddError(nameof(Password1Validated), "Password cannot be empty");
            return;
        }
        ClearErrors(nameof(Password1Validated));
        SetProperty(ref _password1Validated, value);
    }
}
Defensive patterns

Strategy: validation

Validate before calling

if (!string.IsNullOrEmpty(newValue))
    vm.Password1Validated = newValue;
// else surface a validation error to the UI instead of throwing

Type guard

static bool IsValidPassword(string? value) => !string.IsNullOrEmpty(value);

Prevention

When it happens

Trigger: Setting `Password1Validated` to `null` or `""`; clearing the bound password box; initializing/refreshing the view model with an empty value; data binding pushing the empty default of `_password1 = string.Empty` through.

Common situations: The backing field `_password1` is initialized to `string.Empty`, so any binding that round-trips that default into the validated property throws; clearing the field at runtime; binding the validated property to a control whose initial state is empty.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/11c14051fb21181a. Report an issue: GitHub.