2012-11-30 98 views
0

我正在学习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部分包括DataContextCollectionViewSource

<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 NameFile Path代表FilesInfo(字典中的字符串> string>)信息显示?

有没有办法通过XAML和ViewModel Class来完成,而没有Xaml文件背后的代码?

+0

也许这是因为CollectionViewSource检索其值时Folder列表为空。如果你的集合正在改变,你应该考虑实现INotifyPropertyChanged(如果你每次重新分配它)或使用ObservableCollection –

+0

你可以发布你想要的样子的截图吗? – Rhyous

回答

0

您只需要将文件名与文件绑定到文件夹名称属性即可。

对于文件路径,您需要将其绑定到FilesInfo属性。为什么是字典?我想我不明白你为什么在这里使用字典?也许我错过了一些东西,但你应该放弃字典并创建自己的小对象。

public class FileInfo 
{ 
    public string FileName {get;set;} 
    public string FilePath {get;set;} 
} 

然后当然改变你的PatchGen对象来使用它来代替字典。

也许你想要它看起来像一个截图会有所帮助。但是,如果您查看您的XAML,则无法放置您的FolderName。你只有FileName和FilePath的地方。

<ListView Grid.Row="1" 
      HorizontalAlignment="Stretch" 
      Name="lstViewServices" 
      ItemsSource='{Binding Source={StaticResource groups}}'> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn Header="File Name" 
         DisplayMemberBinding="{Binding Path=FileName }" 
         Width="100" /> 
     <GridViewColumn Header="File Path" 
         DisplayMemberBinding="{Binding Path=FilePath}" 
         Width="Auto" /> 
    </GridView> 
    </ListView.View> 
</ListView> 

所以你应该为FolderName添加一个位置。您看起来有两个列表:文件夹列表和每个文件夹的文件列表。但是你的观点只有一个层次。

这是一个有两个级别的例子。

<ItemsControl ItemsSource='{Binding Folders}'> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     <Label Content="{Binding FolderName}" /> 
     <ListView Grid.Row="1" 
        HorizontalAlignment="Stretch" 
        Name="lstViewServices" 
        ItemsSource="FileInfo"> 
      <ListView.View> 
      <GridView> 
       <GridViewColumn Header="File Name" 
           DisplayMemberBinding="{Binding Path=FolderName}" 
           Width="100" /> 
       <GridViewColumn Header="File Path" 
           DisplayMemberBinding="{Binding Path=FolderName }" 
           Width="Auto" /> 
      </GridView> 
      </ListView.View> 
     </ListView> 
     </StackPanel> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

谢谢@Rhyous,我只是想知道是否有办法绑定这种对象。看起来不是。 – Boomer