C#Winform custom control custom attribute panel, C# custom attribute editor, C#winform control custom attribute

Implementation method: Use the custom class RangeConverter inherited from TypeConverter, and the attribute class refers to this converter through the annotation attribute.

Note: If no converter is specified, user-defined classes are generally in design mode and are not editable.

Renderings:

Attributes:

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

Class: 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;
        }
    }

Converter: 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;

        }
    }

 

Guess you like

Origin blog.csdn.net/LoveLearnling/article/details/108347086