【发布时间】:2015-10-28 23:54:08
【问题描述】:
我正在尝试在 DataGrid 的目录中显示文件的路径。为此,在过去的一个小时里,我一直在尝试设置 dataGrid 的 ItemsSource 属性,但无论我做什么,我都会收到 TargetInvocationException。我尝试使用不同类型的集合,甚至尝试使用 Listbox 或 ListView,但没有用。我正在使用 Visual Studio 2015 Community RC。这是我得到的错误:
PresentationFramework.dll 中出现“System.Reflection.TargetInvocationException”类型的未处理异常
附加信息:调用目标已引发异常。
public partial class MainWindow : Window
{
List<FileInfo> fileInfo = new List<FileInfo>();
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
string path = textBox.Text;
if (Directory.Exists(path))
{
string[] fileEntries = Directory.GetFiles(path);
fileInfo.Clear();
foreach (string s in fileEntries)
fileInfo.Add(new FileInfo() { Path = s });
grid.ItemsSource = fileInfo;
}
}
}
public class FileInfo
{
public string Path { get; set; }
}
WPF 代码:
<DataGrid x:Name="grid" Margin="5,30,5,5">
</DataGrid>
【问题讨论】:
标签: c# wpf visual-studio datagrid invocationtargetexception