I made a custom type for gradient colors. I have no problem at design time, but when one of the properties of the custom type is changed at run time, the control has no react on the changes. here is the source code:
------------ Custom Type----------------
[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...
}
}
------------------Type Converter----------------------
public class GradientFillConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) ||
destinationType == typeof(Instan开发者_如何学GoceDescriptor))
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
}
}
I defined a property in my custom control like this:
---------Definition------------
[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.
}
}
}
...
}
Any help would be highly appreciated as it took lots of my time.
Thank you
Calling Refresh()
on control and (or) owner usually helps.
精彩评论