新手学习wpf的treeview!选择一个路径,如何获取该路径下所有特定类型的文件,并将他们绑定在treeview的中
<TreeView Name="tvDirectories" Grid.Column="0"
Margin="6,40,5,5"
MinWidth="500"
MinHeight="400"
Loaded="tvDirectories_Loaded">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type l:FolderTree}" ItemsSource="{Binding Folders}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" HorizontalAlignment="Center"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>   展开
前面:
<Grid>
<TreeView Name="tvDirectories" ItemsSource="{Binding}" >
</TreeView>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="401,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
</Grid>
后台:
private void button1_Click(object sender, RoutedEventArgs e)
{
var list = new List<string>();
string path = @"D:\软件安装程序\应用软件\";//文件夹的路径
if (Directory.Exists(path))//判断要保存的目录文件是否存在。
{
var directory = new DirectoryInfo(path);
FileInfo[] collection = directory.GetFiles("*.exe");//指定类型
foreach (FileInfo item in collection)
{
string fullname = item.Name.ToString();
string filename = fullname.Substring(0, fullname.LastIndexOf("."));//去掉后缀名。
list.Add(filename);
}
tvDirectories.DataContext = list;
}
else
{
MessageBox.Show("文件夹不存在!");
}
}