我想出了以下解决方案:
Private Sub NumericEditor_BindingContextChanged(sender As Object, e As EventArgs) Handles Me.BindingContextChanged
If DataBindings.Count > 0 AndAlso DataBindings.Item("Value") IsNot Nothing Then
Dim myPropDescs As PropertyDescriptorCollection = DataBindings.Item("Value").BindingManagerBase.GetItemProperties
Dim propertyName As String = DataBindings.Item("Value").BindingMemberInfo.BindingField
Dim bindingType As Type = myPropDescs.Find(propertyName, False).PropertyType
Select Case bindingType
Case GetType(SByte)
DecimalPlaces = 0
MinimumValue = SByte.MinValue
MaximumValue = SByte.MaxValue
Case GetType(Byte)
DecimalPlaces = 0
MinimumValue = Byte.MinValue
MaximumValue = Byte.MaxValue
Case GetType(Int16)
DecimalPlaces = 0
MinimumValue = Int16.MinValue
MaximumValue = Int16.MaxValue
Case GetType(UInt16)
DecimalPlaces = 0
MinimumValue = UInt16.MinValue
MaximumValue = UInt16.MaxValue
Case GetType(Int32)
DecimalPlaces = 0
MinimumValue = Int32.MinValue
MaximumValue = Int32.MaxValue
Case GetType(UInt32)
DecimalPlaces = 0
MinimumValue = UInt32.MinValue
MaximumValue = UInt32.MaxValue
Case GetType(Int64)
DecimalPlaces = 0
MinimumValue = Int64.MinValue
MaximumValue = Int64.MaxValue
Case GetType(UInt64)
DecimalPlaces = 0
MinimumValue = UInt64.MinValue
MaximumValue = UInt64.MaxValue
Case GetType(Single), GetType(Double), GetType(Decimal)
MinimumValue = Decimal.MinValue
MaximumValue = Decimal.MaxValue
End Select
End If
End Sub
它有点重复,因此不是那么优雅,但它确实有效。(我的实际代码在设置 MinimumValue 和 MaximumValue 时也会进行检查,以防开发人员已经设置了这些属性,确保开发人员的设置在它们仍然有效的情况下不会被覆盖。)