C#Winform自定义控件自定义属性面板,C#自定义属性编辑器, C#winform控件自定义属性

实现方法:使用继承自TypeConverter的自定义类RangeConverter,属性的类通过注解属性引用此转换器。

注:如果没有指定转换器, 用户自定义的类一般是在设计模式下, 是不可编辑的。

效果图:

属性:

[Description("值")]
public DateTimeRange Value { get { return _value; } set { SetValue(value); } }

类:DateTimeRange:

 [TypeConverter(typeof(RangeConverter))]//使用属性转换器
    public class DateTimeRange 
    {
        private DateRange _thisControl = null;
        private Nullable<DateTime> _value = null;
        private Nullable<DateTime> _value2 = null;
        public void AttatchControl(DateRange control) {
            _thisControl = control;
        }
        public DateTimeRange() {
            value = null;
            value2 = null;
        }
        [Description("值1")]
        public Nullable<DateTime> value { get { return _value; } set { setValue(value); } }
        [Description("值2")]
        public Nullable<DateTime> value2 { get { return _value2; } set { setValue2(value); } }

        public void setValue(Nullable<DateTime> ao_value) {
            _value = ao_value;
            if (!_thisControl.isEmpty()) {
                var lo_value = new DateTimeRange() { value = this._value, value2 = this._value2 };
                lo_value.AttatchControl(_thisControl);
                _thisControl.SetValue(lo_value);
            }
            
        }
        public void setValue2(Nullable<DateTime> ao_value)
        {
            _value2 = ao_value;
            if (!_thisControl.isEmpty())
            {
                var lo_value = new DateTimeRange() { value = this._value, value2 = this._value2 };
                 lo_value.AttatchControl(_thisControl);
                _thisControl.SetValue(lo_value);
            }
        }
        public RangeValue ToRangeValue() {
            RangeValue lo_return = new RangeValue();
            lo_return.value = value;
            lo_return.value2 = value2;

            return lo_return;
        }
    }

转换器:RangeConverter

public class RangeConverter : TypeConverter
    {
        public override PropertyDescriptorCollection
       GetProperties(ITypeDescriptorContext context, object value, Attribute[] filter)
        {
            return TypeDescriptor.GetProperties(value, filter);

        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {

            return true;

        }
    }

猜你喜欢

转载自blog.csdn.net/LoveLearnling/article/details/108347086