【发布时间】:2012-11-30 15:02:31
【问题描述】:
我正在学习 WPF,我正在尝试用文件夹列表(作为 ListView 组)和每个文件夹的文件(作为 ListView 项)填充 ListView。
使用WPF/MVVM Quick Start Tutorial,我创建了以下类(业务已删除)
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; }
}
}
和 ViewModel:
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:
<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 组未显示文件夹名称。 ?
如何让File Name和代表FilesInfo(字典File Path显示出来?
有没有什么方法可以通过 XAML 和 ViewModel 类做到这一点,而不需要 Xaml 文件的代码?
【问题讨论】:
-
可能是因为 CollectionViewSource 检索其值时文件夹列表为空。如果您的集合正在更改,您应该考虑实施 INotifyPropertyChanged(如果您每次都重新分配它)或使用 ObservableCollection
-
你能发一张你想要的截图吗?
标签: c# wpf xaml listview dictionary