System.Windows.Forms.TreeViewを拡張し、独自の実装でノードコレクションを置き換えて、追加のプロパティを追加する独自の拡張System.Windows.Forms.TreeNodeを使用できるようにしました。拡張されたプロセスは、TreeNodeを追加するときに設計時にUIに反映されないという事実を除いて、かなりうまく機能します。私はプロパティをうまく変更することができますが、エディタウィンドウから大丈夫になるとすぐに、変更はインターフェイスに保持されません。デザイナーコードに追加が表示されますが、インターフェイスには何も表示されません。私はこれを成功させるための参考として、以下を使用しています。デザイン時にTreeNodeのTreeNodeCollectionをコードに永続化するにはどうすればよいですか?
http://support.microsoft.com/kb/322937
私は何かが欠けている必要があります。私は、これがwinformsコントロールを拡張するのが難しいとは思っていませんでした。ご協力いただきありがとうございます。気をつけて、素晴らしい一日を。
walkins5
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ExtendedTreeViewControls
{
public class ComplexTreeNodeEditor : System.ComponentModel.Design.CollectionEditor
{
private CollectionForm collectionForm;
public ComplexTreeNodeEditor(Type type) : base(type) { }
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (this.collectionForm != null && this.collectionForm.Visible)
{
ComplexTreeNodeEditor editor = new ComplexTreeNodeEditor(this.CollectionType);
return editor.EditValue(context, provider, value);
}
else
{
return base.EditValue(context, provider, value);
}
}
protected override CollectionForm CreateCollectionForm()
{
this.collectionForm = base.CreateCollectionForm();
return this.collectionForm;
}
protected override object CreateInstance(Type itemType)
{
TreeNodeEx tn = (TreeNodeEx)base.CreateInstance(itemType);
if (this.Context.Instance != null)
{
if (this.Context.Instance is ISupportUniqueName)
{
tn.Name = ((ISupportUniqueName)this.Context.Instance).GetUniqueName();
}
else
{
tn.Name = "TreeNode";
}
}
return tn;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ExtendedTreeViewControls
{
public class TreeNodeCollectionConverter : System.ComponentModel.ExpandableObjectConverter
{
#region Constructors
#endregion
#region Overrides
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType.Equals(typeof(string)))
{
return true;
}
else
{
return base.CanConvertFrom(context, sourceType);
}
//return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
//if (destinationType.Equals(typeof(string)))
//{
// return true;
//}
//else
//{
// return base.CanConvertTo(context, destinationType);
//}
////return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
string txt = (string)value;
return null;//new TreeNodeCollection();//(txt);
}
else
{
return base.ConvertFrom(context, culture, value);
}
//return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
System.Reflection.ConstructorInfo ci =
typeof(TreeNodeEx).GetConstructor(
System.Type.EmptyTypes);
return new InstanceDescriptor(ci, null, false);
}
return base.ConvertTo(context, culture, value, destinationType);
//if (destinationType == typeof(string))
//{
// if (value is ICollection)
// return "(Nodes)";
//}
//else if (destinationType == typeof(InstanceDescriptor))
//{
// return new InstanceDescriptor(typeof(TreeNodeEx).GetConstructor(new Type[] { typeof(int), typeof(string), typeof(TreeNodeEx[]) }), new object[] { ((TreeNodeEx)value).Name, ((TreeNodeEx)value).Name }, true);
//}
//return base.ConvertTo(context, culture, value, destinationType);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
//return true;
bool tb = base.GetPropertiesSupported(context);
return tb;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection col = TypeDescriptor.GetProperties(value);
return col;
//return base.GetProperties(context, value, attributes);
}
#endregion
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ExtendedTreeViewControls
{
[TypeConverter(typeof(TreeNodeCollectionConverter))]
public class TreeNodeCollectionEx : System.Collections.CollectionBase, IEnumerable
{
#region Events
/// <summary>
/// Raised when the collection changes.
/// </summary>
public event EventHandler Changed;
#endregion
#region Class Variables
// Back reference to the parent control
private TreeView parentTree = null;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the TreeNodeCollection using the parent bar.
/// </summary>
/// <param name="parentTree"></param>
public TreeNodeCollectionEx(TreeView parentTree)
{
this.parentTree = parentTree;
}
#endregion
#region Methods
/// <summary>
/// Adds a TreeNode to the collection.
/// </summary>
/// <param name="node">TreeNode to add.</param>
/// <returns>Index of the TreeNode just added in the collection.</returns>
public int Add(TreeNodeEx node)
{
if (Contains(node)) return -1;
int index = InnerList.Add(node);
RaiseChanged();
return index;
}
/// <summary>
/// Adds a range of TreeNodes to the collection.
/// </summary>
/// <param name="nodes">Array of TreeNodes</param>
public void AddRange(TreeNodeEx[] nodes)
{
// Add the array
for (int i = 0; i < nodes.Length; i++)
{
Add(nodes[i]);
}
}
/// <summary>
/// Method to determine if the passed TreeNode is contained in the collection.
/// </summary>
/// <param name="node">TreeNode to test.</param>
/// <returns>True if the TreeNode is in the collection.</returns>
public bool Contains(TreeNodeEx node)
{
return InnerList.Contains(node);
}
/// <summary>
/// Method to obtain the index of a TreeNode in the collection.
/// </summary>
/// <param name="node">TreeNode</param>
/// <returns>Index of the TreeNode in the collection.</returns>
public int IndexOf(TreeNodeEx node)
{
return InnerList.IndexOf(node);
}
/// <summary>
/// Removes a TreeNode from the collection.
/// </summary>
/// <param name="node">The TreeNode to remove.</param>
public void Remove(TreeNodeEx node)
{
if (InnerList.Contains(node))
{
InnerList.Remove(node);
RaiseChanged();
}
}
/// <summary>
/// Inserts a TreeNode in a specific location.
/// </summary>
/// <param name="index">Index in the collection where the TreeNode should be inserted.</param>
/// <param name="node">TreeNode</param>
public void Insert(int index, TreeNodeEx node)
{
// Delegate to base class
InnerList.Insert(index, node);
RaiseChanged();
}
/// <summary>
/// Gets the TreeNode whose index is passed.
/// </summary>
public TreeNodeEx this[int index]
{
get
{
if (index < 0 || index >= Count)
return null;
return (TreeNodeEx)InnerList[index];
}
}
public TreeNodeEx[] GetValues()
{
//It is used by the ComplexItemConverter
TreeNodeEx[] ci = new TreeNodeEx[this.InnerList.Count];
this.InnerList.CopyTo(0, ci, 0, this.InnerList.Count);
return ci;
}
#endregion
#region Implementation
void RaiseChanged()
{
if (Changed != null) Changed(this, null);
}
#endregion
}
}
我々はいくつかのコードを参照する必要があります。標準のTreeNodeで動作する場合、tagプロパティを使用して追加のプロパティを持つカスタムオブジェクトを格納する方が簡単です。 –
UIを構築するときに開発者が使用する複数のプロパティを持つカスタムコントロールを構築するとき、tagプロパティはそれほど役に立ちません。私はいくつかのコードを表示するのが大好きですが、私は複数のクラスファイルを持っていて、このサイトでそれらをどのように表現するかについてはあまりよく分かりません。問題となるかもしれないコードの主な部分は、私が使用しているSystem.ComponentModel.Design.CollectionEditorだと思っています。 – WalkSolutions