|
绑定对象的方法里增加以下代码
else if (DataSource is Array)
{
XmlDocument xml = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(DataSource.GetType());
TextWriter writer = new StringWriter();
serializer.Serialize(writer, DataSource);
writer.Close();
xml.LoadXml(writer.ToString());
NewDataBindToXml(null, xml.DocumentElement.ChildNodes);
}
///绑定方法
private void NewDataBindToXml(TreeNode treeNode, XmlNodeList nodes)
{
foreach (XmlNode node in nodes)
{
var chidnodes = node.ChildNodes;
var name = string.Empty;
var value = string.Empty;
TreePreNodeEventArgs preArgs = new TreePreNodeEventArgs(node);
OnPreNodeDataBound(preArgs);
if (!preArgs.Cancelled)
{
TreeNode childNode = new TreeNode();
if (treeNode == null)
{
Nodes.Add(childNode);
}
else
{
treeNode.Nodes.Add(childNode);
}
foreach (XmlNode n in chidnodes)
{
if (n.Name == _NameFiled)
{
name = n.InnerText;
childNode.Text = name;
}
else if (n.Name == _ValueFiled)
{
value = n.InnerText;
childNode.NodeID = value;
}
else if (n.Name == _ListFiled)
{
if (n.ChildNodes != null)
{
NewDataBindToXml(childNode, n.ChildNodes);
}
}
}
OnNodeDataBound(new TreeNodeEventArgs(childNode, node));
}
}
}
控件增加三个字段
Tree1.ValueFiled = "MenuID";
Tree1.NameFiled = "MenuName";
Tree1.ListFiled = "MenuList";
|
|