PropertyGrid
您可以通过扩展代码来实现这一点。
以下代码使用整数属性。
一步步:
1) 从https://wpftoolkit.codeplex.com/SourceControl/latest下载源扩展 WPF 工具包。
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 值分配给适当的编辑器 ( 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;
}