PenProperties クラスでカスタム コントロールを作成しようとしています。
DashOffSet を除いて、他のすべてのプロパティは、変更するとすぐに文字列を更新します。INotifyPropertyChanged インターフェイスを使用しようとしましたが、うまくいきませんでした。助けていただければ幸いですありがとう!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CircuitSim
{
[TypeConverter(typeof(PenPropertiesConverter))]
public partial class Grid : UserControl
{
private PenProperties _PenProp = new PenProperties();
public Grid()
{
InitializeComponent();
}
public PenProperties Pen
{
get { return _PenProp; }
set
{
_PenProp = value;
}
}
}
}
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace CircuitSim
{
public class GridLinesProperties
{
private bool _Visible;
private int _Frequency;
public PenProperties _PenProp = new PenProperties();
public GridLinesProperties() { }
}
[TypeConverterAttribute(typeof(PenPropertiesConverter))]
public class PenProperties:INotifyPropertyChanged
{
#region private variables
private Color _Color;
private DashCap _DashCap;
private DashStyle _DashStyle;
private int _DashOffset;
private LineCap _EndCap;
private LineCap _StartCap;
#endregion
public PenProperties()
{
_Color = Color.Black;
_DashCap = DashCap.Flat;
_DashStyle = DashStyle.Solid;
_DashOffset = 0;
_EndCap = LineCap.Flat;
_StartCap = LineCap.Flat;
}
public PenProperties(Color color, DashCap dashCap, DashStyle dashstyle, int dashoffset,
LineCap endcap, LineCap startcap)
{
_Color = color;
_DashCap = dashCap;
_DashStyle = dashstyle;
_DashOffset = dashoffset;
_EndCap = endcap;
_StartCap = startcap;
}
#region Properties
public Color Color
{
get { return _Color; }
set
{
_Color = value;
}
}
public DashCap DashCap
{
get { return _DashCap; }
set
{
_DashCap = value;
}
}
public DashStyle DashStyle
{
get { return _DashStyle; }
set
{
_DashStyle = value;
}
}
public int DashOffset
{
get { return _DashOffset; }
set
{
if (this._DashOffset != value)
{
_DashOffset = value;
OnPropertyChanged("DashOffSet");
}
}
}
public LineCap EndCap
{
get { return _EndCap; }
set
{
_EndCap = value;
}
}
public LineCap StartCap
{
get { return _StartCap; }
set
{
_StartCap = value;
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
public class PenPropertiesConverter : TypeConverter
{
public PenPropertiesConverter() { }
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
object value, System.Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(PenProperties));
}
public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, System.Type destinationType)
{
if (destinationType == typeof(string) && value is PenProperties)
{
PenProperties _PenProp = (PenProperties)value;
System.ComponentModel.TypeConverter converter =
System.ComponentModel.TypeDescriptor.GetConverter(_PenProp.Color);
string _Color = converter.ConvertToString(_PenProp.Color);
return _Color + "," + _PenProp.DashCap + "," + _PenProp.DashStyle + "," +
_PenProp.DashOffset + "," + _PenProp.EndCap + "," +
_PenProp.StartCap;
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
string[] v = ((string)value).Split(new char[]{','});
return new PenProperties(Color.FromName(v[0]),
(DashCap)Enum.Parse(typeof(DashCap), v[1]),
(DashStyle)Enum.Parse(typeof(DashStyle),v[2]),
int.Parse(v[3]), (LineCap)Enum.Parse(typeof(LineCap), v[4]),
(LineCap)Enum.Parse(typeof(LineCap), v[5]));
}
return base.ConvertFrom(context, culture, value);
}
}
}