簡陋的樹形控制項,簡陋樹形控制項
<!DOCTYPE html><html><head> <script src="http://code.jquery.com/jquery.min.js"></script> <script> function rootClicked(v) { var newItem=document.createElement("div"); $(newItem).addClass("item"); var Line=document.createElement("div"); $(Line).addClass("line"); var newNode=document.createElement("div"); $(newNode).addClass("node"); var innerBtn=document.createElement("button"); $(innerBtn).html("addNode"); $(innerBtn).attr("onclick","addChild(this)"); var newWrapper=document.createElement("div"); $(newWrapper).addClass("nodeWrapper"); $(newNode).append(innerBtn); $(newItem).append(Line); $(newItem).append(newNode); $(v).parent().next().append(newItem).append(newWrapper); } function addChild(v) { var newItem=document.createElement("div"); $(newItem).addClass("item"); var Line=document.createElement("div"); $(Line).addClass("line"); var newNode=document.createElement("div"); $(newNode).addClass("node"); var innerBtn=document.createElement("button"); $(innerBtn).html("addNode"); $(innerBtn).attr("onclick","addChild(this)"); var newWrapper=document.createElement("div"); $(newWrapper).addClass("nodeWrapper"); $(newNode).append(innerBtn); $(newItem).append(Line); $(newItem).append(newNode); //$(v).parent().parent().parent().children().last().css("background-color","black"); //$(v).parent().parent().next().css("background-color","blue"); //if($(v).parent().parent().next()[0]===$(v).parent().parent().parent().children().last()[0]) //{ // $(newWrapper).css("border-left","white"); //} //else //{ // $(v).parent().parent().parent().children().last().css("border-left","1px dotted #9c9c9c"); //} $(v).parent().parent().next().append(newItem).append(newWrapper); } </script> <title></title> <style> .root { width:100px; height:30px; background-color: #e12249; border:2px solid #a6a6a6; border-radius: 4px; } .item { float:left; clear:both; } .line { width:70px; height:50px; border-bottom:1px dotted #9c9c9c; border-left:1px dotted #9c9c9c; float:left; } .node { width:100px; height:30px; background-color: #e12249; border:2px solid #a6a6a6; border-radius: 4px; float:left; position:relative; top:34px; } .nodeWrapper { float:left;padding-left:120px;clear:both;z-index: 0;border-left: 1px dotted #9c9c9c; } </style></head><body><div style="float:left;"> <div class="root"><button onclick="rootClicked(this)">addNode</button></div> <div style="float:left;margin-left:50px;z-index: 0;"></div></div></body></html>
net樹形控制項的這種效果是怎做出來的?
如果是對 TreeView 實現的話,主要是自訂繪製節點,類似這樣
private void tvSample_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
string text = string.Empty;
if (e.Node.Level == 0)
{
e.DrawDefault = true;
return;
}
else if (e.Node.Parent.Nodes.IndexOf(e.Node) < e.Node.Parent.Nodes.Count - 1)
text = new string(' ', e.Node.Level * 2) + "├" + e.Node.Text;
else
text = new string(' ', e.Node.Level * 2) + "└" + e.Node.Text;
e.Graphics.DrawString(text, tvSample.Font, Brushes.Black, Rectangle.Inflate(e.Bounds, 2, 0));
}
從圖上看這控制項,更像是 asp.net 的 devexpress
怎設計一個樹形控制項?
說簡單呢也簡單,說複雜也複雜。參考treeview自己來做一個控制項其實並不難。多數採取繼承的辦法。
1,開啟vs,選擇控制項陳列庫模板,建立並儲存
2,修改建立的控制項的class名稱與剛剛儲存的名字一致
3,修改puclic建構函式名稱與此前名稱一致
4,修改繼承的類型UserControl為treeview
5,進入控制項.cs,儲存此cs檔案,更改命名空間
6,設計控制項的屬性與方法
7,初步測試並產生控制項
8,加入到工具條並測試控制項