2014-02-22 67 views
1

我有查看SelectedTreeView路徑的問題。TreeView c#filepath

private void btn3_Click(object sender, RoutedEventArgs e) 
    { 
     String TreeViewName = treeView1.SelectedItem.ToString().Replace("Item", String.Empty); 
     MessageBox.Show(TreeViewName); 
    } 

結果是:System.Windows.Controls.TreeView標題:FILENAME s.Count:1

我需要文件的路徑,因爲我有TreeView控件作爲filebrowser的,如果我想從樹視圖運行該文件,它不起作用,因爲它正在寫入結果。

謝謝大家幫忙

地址:

改爲SelectedValuePath後,文本框爲空。你有什麼想法做到這一點嗎?我需要從TreeView運行該文件,之後我需要解決如何從網格或畫布的樹視圖中查看圖片。我的代碼是

private void button2_Click(object sender, EventArgs e) 
    { 
     System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog(); 

     dlg.Description = "Vyberte složku, kterou přidat"; 

     if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      txtbox1.Text = dlg.SelectedPath; 
      ListDirectory(treeView1, dlg.SelectedPath); 
     } 

    } 

    private void ListDirectory(TreeView treeView, string path) 
    { 
     treeView.Items.Clear(); 
     var rootDirectoryInfo = new DirectoryInfo(path); 
     treeView.Items.Add(CreateDirectoryNode(rootDirectoryInfo)); 
    } 

    private static TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo) 
    { 
     var directoryNode = new TreeViewItem { Header = directoryInfo.Name }; 
     foreach (var directory in directoryInfo.GetDirectories()) 
      directoryNode.Items.Add(CreateDirectoryNode(directory)); 

     foreach (var file in directoryInfo.GetFiles()) 
      directoryNode.Items.Add(new TreeViewItem { Header = file.Name }); 

     return directoryNode; 

    } 

    private void btn3_Click(object sender, RoutedEventArgs e) 
    { 
     String TreeViewName = treeView1.SelectedValuePath.ToString().Replace("Item", String.Empty); 
     txtbox2.Text = TreeViewName; 
     System.Diagnostics.Process.Start(TreeViewName); 
    }` 

回答

1

在你的情況,你必須採取的TreeViewItemHeader字符串:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    TreeViewItem SelectedTreeViewItem = SampleTreeView.SelectedItem as TreeViewItem; 
    string FileName = ""; 

    if (SelectedTreeViewItem != null) 
    { 
     FileName = SelectedTreeViewItem.Header.ToString(); // Here 
    } 

    MessageBox.Show(FileName); 
} 
+0

THX非常:) – user3271748