/// <summary> /// helps to move items /// </summary> /// <param name="tree"></param> /// <param name="add"></param> private void AddToTree(TreeView tree, TreeViewItem add) { string control = "", spec = ""; uint cid = 0, spid = 0; if (isRow4Control(add.Header.ToString())) { //pull info control = add.Name.ToString(); int pos = control.IndexOf('f'); cid = uint.Parse(control.Substring(7, pos - 7)); string find = "control" + cid + tree.Name; //look for tree TreeViewItem top = FindName(find) as TreeViewItem; if (top == null) { //create new tree int i = 0, index = -1; TreeViewItem empty = new TreeViewItem(); empty.Expanded += OnExpanded; empty.Name = "control" + cid + tree.Name; RegisterName(empty.Name, empty); empty.Header = add.Header; foreach (object itm in tree.Items) { TreeViewItem tre = itm as TreeViewItem; int fpos = tre.Name.ToString().IndexOf('f'); string bare = tre.Name.Remove(fpos); uint thisid = uint.Parse(bare.Substring(7)); if (thisid == cid) { tre.Header = add.Header; return; } else if (thisid > cid) { index = i; break; } i++; } if (index == -1) { tree.Items.Add(empty); } else { tree.Items.Insert(index, empty); } } else { //already has specs top.Header = add.Header; } } else { //get info int specplace = add.Name.ToString().IndexOf("spec"); string specpiec = add.Name.ToString().Substring(specplace); string piec = add.Name.ToString().Remove(specplace); control = piec; spec = add.Name.ToString(); cid = uint.Parse(control.Substring(7)); int pos = specpiec.IndexOf('f'); spid = uint.Parse(specpiec.Substring(4, pos - 4)); string check = control + tree.Name; TreeViewItem top = FindName(check) as TreeViewItem; if (top == null) { //new spec TreeViewItem newt = new TreeViewItem(); newt.IsExpanded = true; newt.Expanded += OnExpanded; newt.Name = check; RegisterName(check, newt); int i = 0, index = -1; bool added = false; //find olacement foreach (object topcr in tree.Items) { TreeViewItem look = topcr as TreeViewItem; int fpos = look.Name.ToString().IndexOf('f'); string bare = look.Name.Remove(fpos); uint thisid = uint.Parse(bare.Substring(7)); if (thisid > cid) { index = i; added = true; break; } i++; } if (!added) { tree.Items.Add(newt); } else { tree.Items.Insert(index, newt); } top = newt; } spec = control + "spec" + spid + tree.Name; if (top.FindName(spec) == null) { top.IsExpanded = true; TreeViewItem empty = new TreeViewItem(); empty.Expanded += OnExpanded; empty.Name = control + "spec" + spid + tree.Name; empty.Header = add.Header; RegisterName(empty.Name, empty); int i = 0; //find placement foreach (object sp in top.Items) { TreeViewItem specview = sp as TreeViewItem; specplace = add.Name.ToString().IndexOf("spec"); specpiec = specview.Name.ToString().Substring(specplace); pos = specpiec.IndexOf('f'); uint specid = uint.Parse(specpiec.Substring(pos - 4)); if (specid > spid) { top.Items.Insert(i, empty); return; } i++; } top.Items.Add(empty); } } }