グラデーションカラー用のカスタムタイプを作りました。設計時には問題ありませんが、実行時にカスタム タイプのプロパティの 1 つが変更された場合、コントロールは変更に反応しません。ソースコードは次のとおりです。
------------ カスタムタイプ----------------
[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.
}
}
}
...
}
多くの時間がかかったので、どんな助けでも大歓迎です。
ありがとうございました