You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

493 lines
15 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace GeoSigma.SigmaDrawerUtil
{
/// <summary>
/// The custom property.
/// </summary>
public class CustomProperty : INotifyPropertyChanged
{
private int itemOrder = 0; //属性Id
private string theCategory = string.Empty; //属性所属类别
private string theCaption = string.Empty; //属性名称
private bool theReadOnly = false; //属性的只读性true为只读
private string theDescription = string.Empty; //属性的描述内容
private object theValue = null; //值
private System.Type theType = null; //类型
private bool theBrowsable = true; //显示或隐藏true为显示
private TypeConverter theConverter = null; //类型转换
/// <summary>
/// Initializes a new instance of the <see cref="CustomProperty"/> class.
/// </summary>
public CustomProperty()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CustomProperty"/> class.
/// </summary>
/// <param name="fieldName">The field name.</param>
/// <param name="category">The category.</param>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
/// <param name="proType">The pro type.</param>
/// <param name="description">The description.</param>
/// <param name="browsable">If true, browsable.</param>
/// <param name="readOnly">If true, read only.</param>
/// <param name="itemOrder">The item order.</param>
public CustomProperty(string fieldName, string category, string name
, object value, Type proType, string description = null
, bool browsable = true, bool readOnly = false, int itemOrder = 0)
: this()
{
this.Name = fieldName;
ItemOrder = itemOrder;
Category = category ?? string.Empty;
ReadOnly = readOnly;
Caption = name;
Value = value;
Description = description ?? string.Empty;
PropertyType = proType;
Browsable = browsable;
//Converter = converter;
}
[XmlAttribute]
public int ItemOrder
{
get { return itemOrder; }
set { itemOrder = value; }
}
[XmlAttribute]
public string Category
{
get { return theCategory; }
set { theCategory = value; }
}
[XmlAttribute]
public bool ReadOnly
{
get { return theReadOnly; }
set { theReadOnly = value; }
}
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Caption
{
get { return this.theCaption; }
set
{
this.theCaption = value;
}
}
[XmlElement]
public object Value
{
get
{
return this.theValue;
}
set
{
this.theValue = value;
OnPropertyChanged(nameof(Value));
}
}
[XmlAttribute]
public string Description
{
get
{
return theDescription;
}
set
{
theDescription = value;
OnPropertyChanged(nameof(Description));
}
}
//[XmlIgnore]
[XmlIgnore]
public System.Type PropertyType
{
get
{
return theType;
}
set
{
theType = value;
}
}
[XmlAttribute]
public bool Browsable
{
get
{
return theBrowsable;
}
set
{
theBrowsable = value;
}
}
[XmlIgnore]
public virtual TypeConverter Converter
{
get { return theConverter; }
set { theConverter = value; }
}
[XmlIgnore]
public string ParentName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgsEx(propertyName, ParentName));
}
/// <summary>
/// Clones the.
/// </summary>
/// <param name="other">The other.</param>
/// <returns>A CustomProperty.</returns>
public CustomProperty Clone()
{
CustomProperty property = new CustomProperty();
property.Browsable = this.Browsable;
property.Caption = this.Caption;
property.Category = this.Category;
//if (this.Converter != null)
//{
// Type objType = this.Converter.GetType();
// property.Converter = (TypeConverter)Activator.CreateInstance(objType);
//}
//else
//{
// property.Converter = null;
//}
property.Converter = this.Converter;
property.Description = this.Description;
property.ItemOrder = this.ItemOrder;
property.Name = this.Name;
property.ParentName = this.ParentName;
property.PropertyType = this.PropertyType;
property.ReadOnly = this.ReadOnly;
property.Value = this.Value;
return property;
}
}
public class CustomProperties : List<CustomProperty>, ICustomTypeDescriptor
{
#region ICustomTypeDescriptor 成员
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public object GetEditor(System.Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(System.Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public PropertyDescriptorCollection GetProperties(System.Attribute[] attributes)
{
ArrayList props = new ArrayList();
for (int i = 0; i < this.Count; i++)
{ //判断属性是否显示
if (this[i].Browsable == true)
{
CustomPropDescriptor psd = new CustomPropDescriptor(this[i], attributes);
props.Add(psd);
}
}
PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(propArray);
//PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(propArray);
//pdc = pdc.Sort(new PropertyOrderComparer());
//return pdc;
}
public PropertyDescriptorCollection GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
#endregion
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.Count; i++)
{
sb.Append("[" + i + "] " + this[i].ToString() + System.Environment.NewLine);
}
return sb.ToString();
}
}
public class CustomPropDescriptor : PropertyDescriptor
{
private CustomProperty theProp;
public CustomPropDescriptor(CustomProperty prop, Attribute[] attrs)
: base(prop.Caption, attrs)
{
this.theProp = prop;
}
public int PropertyOrder
{
get
{
return this.theProp.ItemOrder;
}
}
public override bool CanResetValue(object component)
{
return false;
}
public override string Category
{
get { return theProp.Category; }
}
public override string Description
{
get { return theProp.Description; }
}
public override TypeConverter Converter
{
get { return theProp.Converter; }
}
public override System.Type ComponentType
{
get { return this.GetType(); }
}
public override object GetValue(object component)
{
return theProp.Value;
}
public override bool IsReadOnly
{
get { return theProp.ReadOnly; }
}
public override System.Type PropertyType
{
get { return theProp.PropertyType; }
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
theProp.Value = value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
//[HostProtection(SecurityAction.LinkDemand, SharedState = true)]
public abstract class ComboBoxItemTypeConvert : TypeConverter
{
public List<KeyValuePair<int, string>> Myhash = null;
public ComboBoxItemTypeConvert()
{
Myhash = new List<KeyValuePair<int, string>>();
GetConvertHash();
}
public abstract void GetConvertHash();
//重写combobox的选择列表
/// <summary>
/// Initializes a new instance of the <see cref="StandardValuesCollectionGetStandardValues"/> class.
/// </summary>
/// <param name="context">The context.</param>
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
int[] ids = new int[Myhash.Count];
for (int i = 0; i < Myhash.Count; i++)
{
ids[i] = i;
}
//int i = 0;
//foreach (string myDE in myhash)
//{
// ids[i++] = i;// (int)(myDE.Key);
//}
return new StandardValuesCollection(ids);
}
//判断转换器是否可以工作
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 obj)
{
if (obj is string)
{
foreach (KeyValuePair<int,string> myDE in Myhash)
{
if (myDE.Value.Equals((obj.ToString())))
return myDE.Key;
}
}
return base.ConvertFrom(context, culture, obj);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
//重写转换器将该类型的值转换到选择列表中
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object obj, Type destinationType)
{
if (destinationType == typeof(string))
{
foreach (KeyValuePair<int, string> myDE in Myhash)
{
if (myDE.Key.Equals(obj))
{
return myDE.Value;
}
}
return "";
}
return base.ConvertTo(context, culture, obj, destinationType);
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
}
public class StringComboItemConvert : ComboBoxItemTypeConvert
{
private List<KeyValuePair<int,string>> hash;
public override void GetConvertHash()
{
try
{
Myhash = hash;
}
catch
{
throw new NotImplementedException();
}
}
public StringComboItemConvert()
{
}
public StringComboItemConvert(string str)
{
hash = new List<KeyValuePair<int, string>>();
string[] stest = str.Split(',');
for (int i = 0; i < stest.Length; i++)
{
hash.Add(new KeyValuePair<int, string>(i, stest[i]));
}
GetConvertHash();
value = 0;
}
//public StringComboItemConvert(string str)
//{
// hash = new Hashtable();
// string[] stest = str.Split(',');
// for (int i = 0; i < stest.Length; i++)
// {
// hash.Add(i, stest[i]);
// }
// GetConvertHash();
// value = 0;
//}
public int value { get; set; }
public StringComboItemConvert(string str, int s)
{
hash = new List<KeyValuePair<int, string>>();
string[] stest = str.Split(',');
for (int i = 0; i < stest.Length; i++)
{
hash.Add(new KeyValuePair<int, string>(i, stest[i]));
}
GetConvertHash();
value = s;
}
}
public class PropertyChangedEventArgsEx : PropertyChangedEventArgs
{
public string ParentName { get; set; }
public PropertyChangedEventArgsEx(string propertyName, string parentName) : base(propertyName)
{
ParentName = parentName;
}
}
}