TreeView
数値を持つ子ノードを保持する Item を持つプログラムがありheaders
ます。に子ノードを追加するときTreeViewItem
、それらを数値で追加したいのですが、他のノードの間にノードを追加する方法がわかりません。
integers
子ノードを並べ替え、ヘッダーを に変換し、ヘッダーを入力された値と比較し、ノード ヘッダーが入力された値よりも大きい場合に停止する関数を呼び出すと思います。新しいノードは、追加される新しいノードよりも大きいヘッダーを持つノードの前に入力する必要があります。
これが最善のアプローチですか、それとももっと良い方法がありますか? これを行う最も簡単な方法を示してください。参考までに、私TreeViewItem
はメイン ウィンドウにあり、別のウィンドウから作業しています。
これにより、子ノードを my に追加する方法がわかりますTreeViewItem
。
//Global boolean
// bool isDuplicate;
//OKAY - Add TreeViewItems to location & Checks if location value is numerical
private void button2_Click(object sender, RoutedEventArgs e)
{
//Checks to see if TreeViewItem is a numerical value
string Str = textBox1.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
//If not numerical value, warn user
if (isNum == false)
MessageBox.Show("Value must be Numerical");
else //else, add location
{
//close window
this.Close();
//Query for Window1
var mainWindow = Application.Current.Windows
.Cast<Window1>()
.FirstOrDefault(window => window is Window1) as Window1;
//declare TreeViewItem from mainWindow
TreeViewItem locations = mainWindow.TreeViewItem;
//Passes to function -- checks for DUPLICATE locations
CheckForDuplicate(TreeViewItem.Items, textBox1.Text);
//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 //else -- create node
{
//Creates TreeViewItems for Location
TreeViewItem newNode = new TreeViewItem();
//Sets Headers for new locations
newNode.Header = textBox1.Text;
//Add TreeView Item to Locations & Blocking Database
mainWindow.TreeViewItem.Items.Add(newNode);
}
}
}
//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;
}
}
ありがとうございました。