2010-12-22 74 views
0

我正在寫一個擴展樹視圖,只是在我的TreeNode類中有一些額外的屬性並帶有designtime支持。擴展treenode/treeview,幾乎在那裏,但

代碼已經準備就緒,但目前我完全停留在以下代碼中。 我在這一點嘗試的一切導致(另一個)例外...

也許任何人有一個想法如何去進一步?我不知道它了

 
using System; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.ComponentModel.Design.Serialization; 
using System.Drawing.Design; 
using System.Globalization; 
using System.Reflection; 
using System.Runtime.Serialization; 
using System.Windows.Forms;

namespace MyProject.Forms { public class MenuTreeView : TreeView { public 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); } }

}

[編輯] 移動樹形目錄到另一個項目之後,一切都很好。不要問我爲什麼...

但是:只有屬性文本和說明被保存,因爲設計器不會爲每個添加的節點創建局部變量。我怎樣才能做到這一點?

最後,我得到它的工作!由於這個:http://netcode.ru/dotnet/?lang=&katID=30&skatID=283&artID=7827

解決的辦法是從TypeConvertor中刪除typeof(字符串),並且當類型是InstanceDescriptor時,只返回默認的構造函數。

+0

你得到了哪些類型的例外?這裏有很多值得注意的地方,例外可能會幫助你(和我們:)縮小一點。 – Steven 2010-12-22 14:32:50

回答

相關問題