PropertyGrid with the RangeAttribute

Grids for WPF Forum

Posted 4 years ago by Frank
Version: 19.1.0684


I'm using the PropertyGrid with a View and a ViewModel like this:

<Window x:Class="TestPropertyGrid.MainWindow"
        Title="MainWindow" Height="450" Width="800">

        <viewModels:MainWindowViewModel />

        <grids:PropertyGrid DataObject="{Binding PropertyGridViewModel}"
                            gridseditors:BuiltinPropertyEditors.IsEnabled="True" />
internal class PropertyGridViewModel : INotifyPropertyChanged
    private float _temperature;

    [Description("The current temperature (Celsius).")]
    [Range(minimum: -10f, maximum: 10f)]
    public float Temperature
        get => _temperature;
            if (Math.Abs(_temperature - value) < 0.01f)

            Validator.ValidateProperty(value, new ValidationContext(this) { MemberName = nameof(Temperature) });

            _temperature = value;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

As you can see, I'm using Data Validation. So, if the Temperature property is set to any value outside of the Range(-10, 10) an error is shown in the PropertyGrid.

I would prefer to set the Minimum and Maximum of the SinglePropertyEditor to prevent the user from entering an out-of-range value. It can be done this way:

<grids:PropertyGrid ...>
        <gridseditors:SinglePropertyEditor Minimum="-10" Maximum="10" />

However, I don't want to hard-code the Minimum and Maximum values in the view for each property of the ViewModel...

Is there a way to make the PropertyGrid to automatically use the RangeAttribute of the properties to set the Minimum and Maximum properties of the corresponding editor (when applicable)?

Or is there any other way to achieve this?



[Modified 4 years ago]

Comments (2)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Frank,

We don't have anything for that at the moment, but looking into it is something on our TODO list.  I'll log your request for that feature with the TODO item.

The problem is that the property model (which is the data context of the property editors) must pass on this information to be bound in a XAML-based property editor data template.  RangeAttribute is tough for a couple reasons.  First, its Minimum and Maximum are stored as Objects instead of specific types like Single.  Second, we probably don't want Minimum and Maximum values passed onto our edit boxes unless they are actually specified via RangeAttribute, which can be tough to do within the context of a DataTemplate.

Actipro Software Support

Posted 4 years ago by Frank

OK, thanks for the answer.

On a similar topic, it would be nice to also support the MaxLengthAttribute (for collections) and the StringLengthAttribute (for strings).

Looking forward for this new feature!

The latest build of this product (v24.1.2) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.