この質問は、この質問のフォローアップです。TreeViewItem
現時点での私の全体的な目標はTreeViewItem
、header
.
ModelView
という、あまりよく知らないツールを使って回答があり、List
のを使えばできるとも言われましたTreeViewItems
。List
の経験が不足しているため、オプションを検討することにしましたModelView
。
私の研究では、 のように実際に参照できないため、Lists
のは少し異なることがわかりました。これにより、作業がより困難になります。現在の方法を説明し、コードを投稿します。私を正しい方向に導き、コーディングソリューションで回答を提供してください。私は現在、自分の機能に行き詰まっています。その領域で何をしようとしているのかについて、コメントに疑似コードを書きました。TreeViewItems
array
treeViewListAdd
*注:TreeViewItem
別のウィンドウからmy に追加しています
現在、私の追加TreeViewItem
プロセスは次のもので構成されています。
- 入力された項目が数値かどうかを確認する(DONE)
if
数値ではない、break
操作(DONE)else
-- 子アイテムの追加を続行します(DONE)- 重複する子をチェック(完了)
if
重複が見つかっbreak
た操作(DONE)else
-- 続行(完了)List
の作成TreeViewItem
(DONE -- しかし実装されていません)TreeViewItem
新しい子ノードの作成(完了)- TVI
header
はtextBox
(DONE)のユーザー テキストから設定されます。 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
}
**/
}
}
助けてくれてありがとう。私はこれに取り組んでおり、自分でできることはすべて試していることを示そうとしました.
リクエストに応じて、これが my の構造ですTreeView
。第 3 レベル以降はすべて、ユーザーによって動的に追加されます...