控件在更改自定义类型属性时不刷新

本文关键字:属性 刷新 类型 自定义 控件 | 更新日期: 2023-09-27 17:59:31

我为渐变色制作了一个自定义类型。我在设计时没有问题,但当自定义类型的某个属性在运行时更改时,控件对更改没有反应。这是源代码:

------------自定义类型------------------

[Serializable]
[TypeConverter(typeof(GradientFillConverter))]
public class GradientFill 
{
    private Color startColor = Color.FromKnownColor(KnownColor.Blue);
    private Color endColor = Color.FromKnownColor(KnownColor.White);
    private int angle = 30;
    public GradientFill()
    {
    }
    public GradientFill(Color startColor, Color endColor, int angle)
    {
        this.startColor = startColor;
        this.endColor = endColor;
        this.angle = angle;
    }
    [NotifyParentProperty(true)]
    [RefreshProperties(RefreshProperties.All)]
    public Color StartColor
    {
        get { return this.startColor; }
        set { this.startColor = value; }
    }
    [NotifyParentProperty(true)]
    [RefreshProperties(RefreshProperties.All)]
    public Color EndColor 
    {
        get { return this.endColor; }
        set { this.endColor = value; }
    }
    [NotifyParentProperty(true)]
    [RefreshProperties(RefreshProperties.All)]
    public int Angle
    {
        get { return this.angle; }
        set { this.angle = value; }
    }
    public static bool operator ==(GradientFill gf1, GradientFill gf2)
    {
// some code...
    }
    public static bool operator !=(GradientFill gf1, GradientFill gf2)
    {
// some code...
    }
    public bool CompareValues(object objectToCompare)
    {
// some code...
    }
    public override bool Equals(object obj)
    {
// some code...
    }
    public override int GetHashCode()
    {
// some code...
    }
}

------------------类型转换器------------------

public class GradientFillConverter : ExpandableObjectConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string) ||
            destinationType == typeof(InstanceDescriptor))
            return true;
        return base.CanConvertTo(context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value != null && value is GradientFill)
        {
            GradientFill gradientFill = (GradientFill)value;
            if (destinationType == typeof(string))
            {
    // returns a string
            }
            if (destinationType == typeof(InstanceDescriptor))
            {
    // returns an Instance Descriptor
            }
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;
        return base.CanConvertFrom(context, sourceType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value != null)
        {
            if (value is string)
            {
    // returns a GradientFill Object
            }
        }
        return base.ConvertFrom(context, culture, value);
    }
    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
    {
        return true;
    }
    public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
    {
    // returns a GradientFill Object
    }
}

我在自定义控件中定义了一个属性,如下所示:

---------定义---------------

[Serializable]
public partial class MyControl : Control
{
...
    private GradientFill backgroundGradient = new GradientFill(Color.FromKnownColor(KnownColor.Blue), Color.FromKnownColor(KnownColor.White), 90);
    public GradientFill BackgroundGradient
    {
        get
        {
            return this.backgroundGradient;
        }
        set
        {
            if (!this.backgroundGradient.CompareValues(value))
            {
                this.backgroundGradient = value;
                this.Repaint(); //Actually invalidates the control.
            }
        }
    }
...
}

任何帮助都将不胜感激,因为这花了我很多时间。

感谢

控件在更改自定义类型属性时不刷新

在控件和(或)所有者上调用Refresh()通常会有所帮助。