本帖最后由 dingxp 于 2014-10-16 22:34 编辑
各位老师,我用wpf做了一个treeview的作业,结果获取不了节点上的文本文件,以下是源码,请帮我指点以下好吗:
C#源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace 福尔摩斯全集treeview
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string path = "福尔摩斯小说全集";
TreeViewItem tvi = new TreeViewItem() { Header = path };
Loaditem(path,tvi);
TV.Items.Add(tvi);
}
void Loaditem(string path,TreeViewItem tvi)
{
string[] strs = Directory.GetDirectories(path);
for (int i = 0; i < strs.Length; i++)
{
string temppath = strs;
TreeViewItem temp = new TreeViewItem() { Header = System.IO.Path.GetFileName(strs) };
tvi.Items.Add(temp);
Loaditem(temppath, temp);
}
string[] texts =Directory.GetFiles(path);
for (int i = 0; i < texts.Length; i++)
{
TreeViewItem tvi1 = new TreeViewItem();
tvi1.Tag = texts;
tvi1.Items.Add(texts);
tvi.Items.Add(System.IO.Path.GetFileName(texts));
//tvi.Items. = "texts";
}
}
private void TV_selected(object sender, RoutedEventArgs e)
{
TreeViewItem tvi = new TreeViewItem();
tvi = e.OriginalSource as TreeViewItem;
if (tvi.Tag==null)
{
return;
}
string taag = (string)tvi.Tag;
TT.Text = File.ReadAllText(taag);
}
}
}
xaml源码:
<Window x:Class="福尔摩斯全集treeview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<DockPanel LastChildFill="True">
<TreeView TreeViewItem.Selected="TV_selected" Name="TV" Width="130" ></TreeView>
<TextBox Name="TT"></TextBox>
</DockPanel>
</Grid>
</Window>
|
|