PropertyGrid with the RangeAttribute

Grids for WPF Forum

Posted 4 years ago by Frank
Version: 19.1.0684
Avatar

Hello,

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

<Window x:Class="TestPropertyGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:viewModels="clr-namespace:TestPropertyGrid.ViewModels"
        xmlns:grids="http://schemas.actiprosoftware.com/winfx/xaml/grids"
        xmlns:gridseditors="http://schemas.actiprosoftware.com/winfx/xaml/gridseditors"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <viewModels:MainWindowViewModel />
    </Window.DataContext>

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

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

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

            _temperature = value;
            RaisePropertyChanged();
        }
    }

    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 ...>
    <grids:PropertyGrid.PropertyEditors>
        <gridseditors:SinglePropertyEditor Minimum="-10" Maximum="10" />
    </grids:PropertyGrid.PropertyEditors>
</grids:PropertyGrid>

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?

Thanks

Frank

[Modified 4 years ago]

Comments (2)

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

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
Avatar

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 5 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.