MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · Exception

Value must be positive

Error message

Value must be positive

What it means

A demo ViewModel (`MahViewModel`, part of the MahApps/MaterialDesign mash-up sample) throws a base `System.Exception` from the `UpDownValue` setter whenever the incoming value is negative. It is sample code meant to illustrate validation in an integer up/down control bound through change notification; the value is expected to stay non-negative.

Source

Thrown at src/MahMaterialDragablzMashUp/MahViewModel.cs:24

public class MahViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    public ObservableCollection<GridRowData> GridData { get; }

    private int _UpDownValue;
    public int UpDownValue
    {
        get
        {
            return _UpDownValue;
        }
        set
        {
            if (value < 0)
            {
                throw new Exception("Value must be positive");
            }
            if (_UpDownValue != value)
            {
                _UpDownValue = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UpDownValue)));
            }
        }
    }

    public MahViewModel()
    {
        GridData = new ObservableCollection<GridRowData> {
            new GridRowData {
                IsChecked = false,
                Text = "Mars",
                EnumValue = EnumValues.ValueA,
                IntValue = 4879
            },

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Clamp the bound control's Minimum to 0 so the setter never receives a negative value.
  2. If negatives are now legitimate, relax the guard (remove the `if (value < 0)` block) or raise it to a domain-correct bound.
  3. Replace the base `throw new Exception(...)` with `throw new ArgumentOutOfRangeException(nameof(UpDownValue), value, "Value must be positive")` so callers can catch a specific type.
  4. For input validation prefer WPF `IDataErrorInfo`/`INotifyDataErrorInfo` over throwing from a setter, which breaks two-way binding.

Example fix

// before
if (value < 0)
{
    throw new Exception("Value must be positive");
}

// after
value = Math.Max(0, value);
if (_UpDownValue != value)
{
    _UpDownValue = value;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UpDownValue)));
}
Defensive patterns

Strategy: validation

Validate before calling

// Before assigning, ensure non-negative
int candidate = GetValueFromControl();
if (candidate < 0) candidate = 0;
vm.UpDownValue = candidate;

Prevention

When it happens

Trigger: Assigning a negative integer to `MahViewModel.UpDownValue` (e.g. from a NumericUpDown control whose minimum was not clamped, or programmatically setting the property below zero).

Common situations: Removing/loosening the Minimum on the bound NumericUpDown control, driving the property from a slider or test code that goes below 0, or copying this sample pattern into production where user input can be negative.

Related errors


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