Setting range on auto generated Int32Editor

Grids for WPF Forum

The latest build of this product (v21.1.1) was released 3 months ago, which was before this thread was created.
Posted 28 days ago by John Dunn
Version: 19.1.0687
Avatar

I have a data model that has a couple of ints, each of which have their own range. I'm setting DataObject to the model and have a custom TypeDescriptorFactory to generate the IPropertyModels. What would be the best way to set the range on the generated Int32Picker? I know I can validate the input after it's been entered but I think a better experience would be to limit the control range from the start.

Comments (3)

Answer - Posted 28 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi John,

Int32PropertyEditor has Minimum and Maximum properties you can use that get applied to the edit box.  If you have fixed min/max values for a certain property, you can create an Int32PropertyEditor and add it to the PropertyGrid.PropertyEditors collection, using various type and name combinations to target it, like:

<gridseditors:Int32PropertyEditor PropertyName="Percentage" Minimum="0" Maximum="100" />

This topic describes the property editor matching procedure.

If instead the min/max can dynamically change, you'd have to make a custom property editor and use an Int32EditBox in its DataTemplate.  Then bind the Int32EditBox.Minimum and Maximum to appropriate values accessible via the IPropertyModel data context.


Actipro Software Support

Posted 28 days ago by John Dunn
Avatar

Thank you - that worked. It also looks likes it works adding PropertyEditors in C#. I tried an experiment and modified the range of the Int32PropertyEditor dynamically directly from code and it seemed to work to dynamically change the range without creating custom editor. Is this a bad idea?

Here's my test code

using ActiproSoftware.Windows;
using ActiproSoftware.Windows.Controls.Editors.Interop.Grids.PropertyEditors;
using ActiproSoftware.Windows.Controls.Grids.PropertyData;
using System.ComponentModel;
using System.Windows;

namespace WpfApp3
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      var pe = new Int32PropertyEditor() { PropertyName = "Value", Minimum = 100, Maximum = 255 };
      theGrid.PropertyEditors.Add(pe);

      DataContext = new Data()
      {
        Test = pe,
        Name = "this is a name",
        Value = 234,
        Option = Options.No
      };
    }
  }

  public enum Options { Yes, No, Maybe };

  public class Data : ObservableObjectBase, IPropertyOwner
  {
    public Int32PropertyEditor Test { get; set; }

    [Category("General")]
    [DisplayName("A Name")]
    public  string Name { get; set; }
    [Category("General")]
    [DisplayName("Some Value")]
    public int Value { get; set; }


    Options option;
    [Category("General")]
    [DisplayName("Some Options")]
    public Options Option
    {
      get => option;
      set
      {
        if(option != value)
        {
          option = value;
          if (option == Options.Maybe) Test.Minimum = 0;
          else Test.Minimum = 50;
        }
      }
    }

    bool showIt;
    public bool ShowIt
    {
      get => showIt;
      set
      {
        if( showIt != value)
        {
          showIt = value;
          NotifyPropertyChanged("ShowIt");
          NotifyPropertyChanged("It");
        }
      }
    }

    int it;
    public int It
    {
      get => it;
      set
      {
        if( it != value)
        {
          it = value;
          this.NotifyPropertyChanged("It");
        }
      }
    }

    public bool GetPropertyVisibility(string name)
    {
      if (name == "It") return ShowIt;
      return true;
    }
  }

  interface IPropertyOwner
  {
    bool GetPropertyVisibility(string name);
  }

  public class CustomPropertyModel : PropertyDescriptorPropertyModel
  {
    public CustomPropertyModel(object target, PropertyDescriptor propertyDescriptor) : base(target, propertyDescriptor) { }
    public bool IsVisible
    {
      get
      {
        if (this.Target is IPropertyOwner owner) return owner.GetPropertyVisibility(this.Name);
        return true;
      }
    }
    bool updating = false;
    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
      // Call the base method
      base.OnPropertyChanged(e);
      if (!updating)
      {
        updating = true;
        this.NotifyPropertyChanged("IsVisible");
        updating = false;
      }
    }
  }

  public class CustomDataFactory : TypeDescriptorFactory
  {
    protected override IPropertyModel CreatePropertyModel(object target, PropertyDescriptor propertyDescriptor, IDataFactoryRequest request)
    {
      var prop =  new CustomPropertyModel(target, propertyDescriptor);
      return prop;
    }
  }
}
Posted 28 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi John,

It should work programmatically as long as the property editor min/max values are set prior to the PropertyGrid data being loaded (i.e. via your DataContext).


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.