我正在学习WPF,我试图用列表文件夹(作为ListView组)和每个文件夹(作为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; }
}
}
和视图模型:
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
和File Path
代表FilesInfo
(字典中的字符串> string>)信息显示?
有没有办法通过XAML和ViewModel Class来完成,而没有Xaml文件背后的代码?
也许这是因为CollectionViewSource检索其值时Folder列表为空。如果你的集合正在改变,你应该考虑实现INotifyPropertyChanged(如果你每次重新分配它)或使用ObservableCollection –
你可以发布你想要的样子的截图吗? – Rhyous