我正在学习 WPF,我试图用文件夹列表(作为 ListView 组)和每个文件夹的文件(作为 ListView 项目)填充 ListView。
使用WPF/MVVM 快速入门教程,我创建了以下类(业务已删除)
public class PatchGen
{
public PatchGen() { }
private string _folderName;
private Dictionary<string, string> _filesInfo = new Dictionary<string, string>();
public string FolderName
{
get { return _folderName; }
set { _folderName= value; }
}
public Dictionary<string, string> FilesInfo
{
get { return _filesInfo; }
set { _filesInfo = value; }
}
}
和视图模型:
public class PatchGenViewModel : ObservableObject
{
public PatchGenViewModel()
{
}
List<PatchGen> _folderList = new List<PatchGen>();
public List<PatchGen> Folders
{
get
{
return _folderList;
}
set { }
}
void AddFilesExecute()
{
//business here
}
bool CanAddFilesExecute()
{
return true;
}
public ICommand AddFiles { get { return new RelayCommand(AddFilesExecute, CanAddFilesExecute); } }
xaml 部分包括DataContext
和CollectionViewSource
:
<Window.DataContext>
<local:PatchGenViewModel></local:PatchGenViewModel>
</Window.DataContext>
<Window.Resources>
<CollectionViewSource x:Key='groups'
Source="{Binding Path=Folders}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="FolderName" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
和列表视图:
<ListView Grid.Row="1"
HorizontalAlignment="Stretch"
Name="lstViewServices"
ItemsSource='{Binding Source={StaticResource groups}}'>
<ListView.View>
<GridView>
<GridViewColumn Header="File Name"
DisplayMemberBinding="{Binding Path=??? }"
Width="100" />
<GridViewColumn Header="File Path"
DisplayMemberBinding="{Binding Path=??? }"
Width="Auto" />
</GridView>
</ListView.View>
</ListView>
ListView 组未显示文件夹名称。?
如何显示代表(Dictionnary < string,string > ) 信息的File Name
and ?File Path
FilesInfo
有没有办法通过 XAML 和 ViewModel 类来做到这一点,而不需要 Xaml 文件的代码?