0

我正在编写一个扩展的树视图,只是在我的 TreeNode 类中带有一些额外的属性,并支持设计时。

代码已经准备就绪,但目前我完全被以下代码所困扰。我在这一点上尝试的一切都会导致(另一个)异常......

也许有人知道如何走得更远?我不知道了

使用系统;
使用 System.ComponentModel;
使用 System.ComponentModel.Design;
使用 System.ComponentModel.Design.Serialization;
使用 System.Drawing.Design;
使用 System.Globalization;
使用 System.Reflection;
使用 System.Runtime.Serialization;
使用 System.Windows.Forms;

命名空间 MyProject.Forms { 公共类 MenuTreeView : 树视图 { 公共菜单树视图() { // }

[Editor(typeof(MenuTreeNodeCollectionEditor), typeof(UITypeEditor))] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public new TreeNodeCollection Nodes { get { return base.Nodes; } } } [Serializable] [DefaultProperty("Text")] [TypeConverter(typeof(MenuTreeNodeConverter))] public class MenuTreeNode : TreeNode, ISerializable { private string description = ""; public MenuTreeNode() : base() { // } public MenuTreeNode(string text) : base(text) { // } public MenuTreeNode(string text, string description) : base(text) { this.description = description; } public MenuTreeNode(string text, MenuTreeNode[] children) : base(text, children) { // } public override object Clone() { object clone = base.Clone(); MenuTreeNode node = clone as MenuTreeNode; if (node != null) { node.Description = Description; return node; } else return clone; } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { Serialize(info, context); } protected override void Deserialize(SerializationInfo info, StreamingContext context) { Description = info.GetString("Description"); base.Deserialize(info, context); } protected override void Serialize(SerializationInfo info, StreamingContext context) { info.AddValue("Description", Description); base.Serialize(info, context); } [DefaultValue("")] public string Description { get { return description; } set { description = value; } } } public class MenuTreeNodeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type type) { if (type == typeof(string)) { return true; } return base.CanConvertFrom(context, type); } public override bool CanConvertTo(ITypeDescriptorContext context, Type type) { if (type == typeof(InstanceDescriptor) || type == typeof(string)) { return true; } return base.CanConvertTo(context, type); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo info, object value) { if (value != null && value is string) { string[] items = ((string)value).Split(','); return new MenuTreeNode(items[0], items[1]); } return base.ConvertFrom(context, info, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo info, object value, Type type) { MenuTreeNode node = value as MenuTreeNode; if (value != null) { if (type == typeof(string)) { return node.Text + "," + node.Description; } else if (type == typeof(InstanceDescriptor)) { ConstructorInfo constructor = typeof(MenuTreeNode).GetConstructor(new Type[] { typeof(string), typeof(string) }); return new InstanceDescriptor(constructor, new object[] { node.Text, node.Description }, true); } } return base.ConvertTo(context, info, value, type); } } public class MenuTreeNodeCollectionEditor : CollectionEditor { public MenuTreeNodeCollectionEditor(Type t) : base(t) { // } protected override Type CreateCollectionItemType() { return typeof(MenuTreeNode); } protected override Type[] CreateNewItemTypes() { return new Type[] { typeof(MenuTreeNode) }; } protected override object CreateInstance(Type itemType) { if (itemType == typeof(MenuTreeNode)) { return new MenuTreeNode(); } return base.CreateInstance(itemType); } protected override string GetDisplayText(object value) { MenuTreeNode node = value as MenuTreeNode; if (node != null) { return "MenuTreeNode: " + node.Text; } return base.GetDisplayText(value); } }

}

[编辑] 将 Treeview 移动到另一个项目后,一切正常。不要问我为什么...

但是:只保存属性文本和描述,因为设计器不会为每个添加的节点创建局部变量。我怎样才能做到这一点?

[编辑] 最后,我让它工作了!多亏了这个:http ://netcode.ru/dotnet/?lang=&katID=30&skatID=283&artID=7827

解决的办法是把TypeConvertor中的typeof(string)去掉,当type为InstanceDescriptor时,只返回默认的构造函数。

4

1 回答 1

0

最后,我让它工作了!感谢这篇文章:http ://netcode.ru/dotnet/?lang=&katID=30&skatID=283&artID=7827

解决方案是从 TypeConvertor 中删除 typeof(string),当类型为 InstanceDescriptor 时,只需返回默认构造函数即可。

于 2010-12-23T12:20:24.563 回答