如何在C#中为PropertyGrid项设置长度?

5
例如我有2个项目:Item1是string,Item2是int。我该如何在C#中为Item1设置最大长度为8个字符,为Item2设置最大长度为5个数字?

如何通过设置属性来抛出异常呢? - Marc Gravell
我只想为item2设置小于1000的数字,我该怎么做?(当输入4个字符时,item2不允许输入超过3位数) - Ali Ahmadi
1
据我所知,PropertyGrid不支持那种类型的场景。 - Marc Gravell
1个回答

0

要做到这一点,只需使用StringLengthAttributeIntegerValidatorAttribute

class TestCase
{
  [StringLength(8, ErrorMessage = "The TestString value cannot exceed 8 characters.")]
  [Required(ErrorMessage="Value Required")]
  property string TestString;

  [IntegerValidator(MaxValue = 99999)]
  property int TestInt;
}

3
这似乎不会影响属性网格对属性的处理。 - BlueMonkMN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接