Wpf PropertyGrid Min / Max attrs

I am using the WPF Extended Toolkit PropertyGrid for many types of objects. Objects are wrappers for configurations. Many properties are integers, and I want to define the minimum / maximum range of a particular property in a class definition.

Something like that:

    [Category("Basic")]
    [Range(1, 10)]
    [DisplayName("Number of outputs")]
    public int NumberOfOutputs
    {
        get { return _numberOfOutputs; }
        set 
        {
            _numberOfOutputs = value;
        }
    }

Is there any solution to achieve this? I think that this is possible with the help of a special PropertyGrid editor, but I mean that it is unnecessarily complicated.

Many thanks!

+4
source share
1 answer

You can achieve this by expanding the PropertyGridcode.

The following code works with integer properties.

Step by step:

1) WPF Toolkit https://wpftoolkit.codeplex.com/SourceControl/latest.

2) Xceed.Wpf.Toolkit .

3) RangeAttribute :

namespace Xceed.Wpf.Toolkit.PropertyGrid.Implementation.Attributes
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class RangeAttribute : Attribute
    {
        public RangeAttribute(int min, int max)
        {
            Min = min;
            Max = max;
        }

        public int Min { get; private set; }
        public int Max { get; private set; }
    }
}

4) ObjectContainerHelperBase, Min Max appriopriate (IntegerUpDown). GenerateChildrenEditorElement, :

private FrameworkElement GenerateChildrenEditorElement( PropertyItem propertyItem )
{
  FrameworkElement editorElement = null;
  DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition;
  object definitionKey = null;
  Type definitionKeyAsType = definitionKey as Type;

  ITypeEditor editor = pd.CreateAttributeEditor();
  if( editor != null )
    editorElement = editor.ResolveEditor( propertyItem );


  if( editorElement == null && definitionKey == null )
    editorElement = this.GenerateCustomEditingElement( propertyItem.PropertyDescriptor.Name, propertyItem );

  if( editorElement == null && definitionKeyAsType == null )
    editorElement = this.GenerateCustomEditingElement( propertyItem.PropertyType, propertyItem );

  if( editorElement == null )
  {
    if( pd.IsReadOnly )
      editor = new TextBlockEditor();

    // Fallback: Use a default type editor.
    if( editor == null )
    {
      editor = ( definitionKeyAsType != null )
      ? PropertyGridUtilities.CreateDefaultEditor( definitionKeyAsType, null )
      : pd.CreateDefaultEditor();         
    }

    Debug.Assert( editor != null );

    editorElement = editor.ResolveEditor( propertyItem );

      if(editorElement is IntegerUpDown)
      {
          var rangeAttribute = PropertyGridUtilities.GetAttribute<RangeAttribute>(propertyItem.DescriptorDefinition.PropertyDescriptor);
          if (rangeAttribute != null)
          {
              IntegerUpDown integerEditor = editorElement as IntegerUpDown;
              integerEditor.Minimum = rangeAttribute.Min;
              integerEditor.Maximum = rangeAttribute.Max;
          }
      }
  }

  return editorElement;
}
+4

All Articles