问题描述
在我的页面中,我使用了 Listview
,它显示目录中的文件名。
当我在 TextCell
中使用 Listview
时,Listview
项为空。
我的 Listview
项代码是:
DirectoryInfo di = new DirectoryInfo(myDirectory);
FileInfo[] files = di.GetFiles();
if (Directory.Exists(myDirectory))
{
for (int i = 0; i < files.Length; i++)
{
lstFiles.Add(i + 1 + ". " + files[i].Name);
}
}
lstviewsFile.ItemsSource = lstFiles;
我的 xaml:
-
有效的xaml
<ListView x:Name="lstviewsFile" ItemsSource="{Binding lstFiles}" SeparatorColor="#448AFF" SelectionMode="Single" ItemSelected="lstviewsFile_ItemSelected"> </ListView>
-
xaml 不起作用
<ListView x:Name="lstviewsFile" SeparatorColor="#448AFF" SelectionMode="Single" ItemSelected="lstviewsFile_ItemSelected"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding lstFiles}" TextColor="#FFFFFF"></TextCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
你能帮我吗?
解决方法
lstFiles
是您的 ItemsSource
- 列表中的每一行只是一个字符串(大概,您实际上并未显示该代码)
所以你的绑定应该是
<TextCell Text="{Binding .}" ... />