我正在學習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