如何在wpf中使用folderbrowserdialog将路径目录中的文件列出到一个列数据网格中
本文关键字:网格 数据网 数据 一个 文件 wpf folderbrowserdialog 路径 | 更新日期: 2023-09-27 18:03:38
下面的代码是我的一个浏览按钮的功能。
我能知道我应该做什么在列中列出的文件在datagrid?
private void button1_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
textBox1.Text = dialog.SelectedPath;
}
}
这可能就是你要找的:
Xaml:<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid x:Name="myGrid" Grid.Row="0" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Files" Binding="{Binding}"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="1" Content="SetList" Click="OnClick" />
</Grid>
背后的代码:
private void OnClick(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
myGrid.ItemsSource = System.IO.Directory.GetFiles(dialog.SelectedPath);
}
}
}
您可以像这样列出文件:
private void button1_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
textBox1.Text = dialog.SelectedPath;
string[] files = Directory.GetFiles(dialog.SelectedPath);
}
}