クラスNodeでTreeViewItemを作成します。この例では、ノードはソースコードで指定されています。C#WPF:テキストファイルからツリービューを作成
任意のアイデア:ノードがこのような内容のテキストファイルからインポートされる場合しかし、私はそれをどのように行うのですか?
私は以下を試しました。
public MainWindowVM()
{
private ObservableCollection<Node> mRootNodes;
public IEnumerable<Node> RootNodes { get { return mRootNodes; } }
List<string[]> TreeNodes = new List<string[]>();
string[] lines = null;
try
{
lines = System.IO.File.ReadAllLines(MainWindow.TextFilePath , System.Text.Encoding.Default);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
Environment.Exit(0);
}
if (lines == null || lines.Length == 0)
{
MessageBox.Show("Text file has no content!");
Environment.Exit(0);
}
foreach (var line in lines)
{
TreeNodes.Add(line.Split('|'));
}
Node newNode = null;
Node childNode = null;
Node root = new Node() { Name = TreeNodes[0][0] };
if (TreeNodes[0].Length > 1)
{
newNode = new Node() { Name = TreeNodes[0][1] };
root.Children.Add(newNode);
}
for (int s = 2; s < TreeNodes[0].Length; s++)
{
childNode = new Node() { Name = TreeNodes[0][s] };
newNode.Children.Add(childNode);
newNode = childNode;
}
}
しかし、私は最初の2つのノードだけを取得します。私はどのようにループ全体TreeViewを構築するか分からない。
ループを必要としています。あなたは** text **として投稿して頂けますし、質問に 'Node'クラスのコードを含めることもできます(間違ったリンク) – ASh
" Node "の上をクリックしてください。 リンクを修正しました。 – sanjar14