This question is a follow up to this question. My overall goal at the moment is to add to my program's TreeViewItem
(my TreeViewItem
has child nodes added to it at run-time) in numerical ascending order according to the value entered in for the header
.
I received an answer using a ModelView
, a tool that I am not all that familiar with, and I was also told that this could be done by using a List
of TreeViewItems
. I have decided to explore the List
option due to my lack of experience with ModelView
.
In my research I've learned that Lists
of TreeViewItems
are a little different, because you really can't reference them like you can an array
. This makes them more difficult to do work with. I'll explain my current method and post my code. Please steer me in the right direction and provide answers with coding solutions. I'm currently stuck on my treeViewListAdd
function. I have written pseudo code in comments on what I am trying to do with that area.
*Note: I am adding to my TreeViewItem
from a separate window
Right now my add TreeViewItem
process consists of:
- Check to see if item entered is numerical (DONE)
if
not numerical,break
operation (DONE)else
-- continue with adding child item (DONE)- Check for duplicate children (DONE)
if
duplicate is foundbreak
operation (DONE)else
-- continue (DONE)- Create
List
ofTreeViewItem
(DONE -- But not implemented) - Create
TreeViewItem
for new child node (DONE) - TVI
header
is set from user text intextBox
(DONE) - Pass to function that attempts to add to the
List
in numerical order (Problem Area) - Add sorted
List
toTreeViewItem
in main window (Problem Area)
My current code:
//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
}
**/
}
}
Thanks a lot for the help. I tried to show that I have been working on this and trying everything that I could on my own.
As requested, here is the structure of my TreeView
. Everything from the 3rd level and down is dynamically added by the user...