这个问题是这个问题的后续。我目前的总体目标是TreeViewItem
根据TreeViewItem
为header
.
我收到了一个使用 a 的答案ModelView
,这是一个我不太熟悉的工具,我还被告知可以通过使用 a 来List
完成TreeViewItems
。List
由于缺乏经验,我决定探索该选项ModelView
。
在我的研究中,我了解到Lists
ofTreeViewItems
有点不同,因为你真的不能像array
. 这使他们更难与他们合作。我将解释我当前的方法并发布我的代码。请引导我朝着正确的方向前进,并提供编码解决方案的答案。我目前被困在我的treeViewListAdd
功能上。我已经在评论中写了伪代码,说明我想在该区域做什么。
*注意:我TreeViewItem
从一个单独的窗口添加到我的
现在我的添加TreeViewItem
过程包括:
- 检查输入的项目是否为数字(完成)
if
非数字,break
操作(完成)else
-- 继续添加子项(DONE)- 检查重复的孩子(完成)
if
发现重复break
操作(DONE)else
-- 继续(完成)- 创建
List
(TreeViewItem
完成——但未实施) TreeViewItem
为新的子节点创建(完成)- TVI从(DONE)
header
中的用户文本设置textBox
- 传递给试图添加到
List
数字顺序的函数(问题区域) List
在TreeViewItem
主窗口中添加排序(问题区域)
我当前的代码:
//OKAY - Add child to TreeViewItem in Main Window
private void button2_Click(object sender, RoutedEventArgs e)
{
//STEP 1: Checks to see if entered text is a numerical value
string Str = textBox1.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
//STEP 2: If not numerical value, warn user
if (isNum == false)
MessageBox.Show("Value must be Numerical");
else //STEP 3: else, continue
{
//close window
this.Close();
//Query for Window1
var mainWindow = Application.Current.Windows
.Cast<Window1>()
.FirstOrDefault(window => window is Window1) as Window1;
//STEP 4: Check for duplicate
//declare TreeViewItem from mainWindow
TreeViewItem locations = mainWindow.TreeViewItem;
//Passes to function -- checks for DUPLICATE locations
CheckForDuplicate(locations.Items, textBox1.Text);
//STEP 5: if Duplicate exists -- warn user
if (isDuplicate == true)
MessageBox.Show("Sorry, the number you entered is a duplicate of a current Node, please try again.");
else //STEP 6: else -- create child node
{
//STEP 7
List<TreeViewItem> treeViewList = new List<TreeViewItem>();
//STEP 8: Creates child TreeViewItem for TVI in main window
TreeViewItem newLocation = new TreeViewItem();
//STEP 9: Sets Headers for new child nodes
newLocation.Header = textBox1.Text;
//STEP 10: Pass to function -- adds/sorts List in numerical ascending order
treeViewListAdd(ref treeViewList, newLocation);
//STEP 11: Add children to TVI in main window
//This step will of course need to be changed to add the list
//instead of just the child node
mainWindow.TreeViewItem.Items.Add(newLocation);
}
}
}
//STEP 4: Checks to see whether the header entered is a DUPLICATE
private void CheckForDuplicate(ItemCollection treeViewItems, string input)
{
for (int index = 0; index < treeViewItems.Count; index++)
{
TreeViewItem item = (TreeViewItem)treeViewItems[index];
string header = item.Header.ToString();
if (header == input)
{
isDuplicate = true;
break;
}
else
isDuplicate = false;
}
}
//STEP 10: Adds to the TreeViewItem list in numerical ascending order
private void treeViewListAdd(ref List<TreeViewItem> currentList, TreeViewItem addLocation)
{
//if there are no TreeViewItems in the list, add the current one
if (currentList.Count() == 0)
currentList.Add(addLocation);
else
{
//gets the index of the last item in the List
int lastItem = currentList.Count() - 1;
/*
if (value in header > lastItem)
currentList.Add(addLocation);
else
{
//iterate through list and add TreeViewItem
//where appropriate
}
**/
}
}
非常感谢您的帮助。我试图表明我一直在努力,并尽我所能尝试一切。
根据要求,这是我的TreeView
. 从第 3 级及以下的所有内容都由用户动态添加...