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 5 years ago]