自定义控制属性通知

时间:2012-11-30 14:21:37

标签: c# visual-studio-2010 properties inotifypropertychanged

我正在尝试使用PenProperties类创建自定义控件。

Properties

除了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);
    }

}

}

1 个答案:

答案 0 :(得分:1)

您的DashOffset属性框与您正在引发的OnPropertyChanged事件不同。 OffSet中的's'在OnPropertyChanged中大写,但不在属性本身的名称中