2012-12-03 51 views
0

我創造的服務名稱分組一個ListView,但我不能夠顯示文件名文件路徑,這裏的類和XAML中:WPF綁定:另一個列表內的列表值

public class SOService 
{ 
    string _ServiceName; 
    List<SOFileInfo> _SOFiles; 

    public string ServiceName 
    { 
     get { return _ServiceName; } 
     set { _ServiceName = value; } 
    } 
    public List<SOFileInfo> SOFiles 
    { 
     get { return _SOFiles; } 
     set { _SOFiles = value; } 
    } 
} 

public class SOFileInfo 
{ 
    string _FileName; 
    string _FilePath; 

    public string FileName 
    { 
     get 
     { return _FileName; } 
     set { _FileName = value; } 
    } 
    public string FilePath 
    { 
     get { return _FilePath; } 
     set { _FilePath = value; } 
    } 
} 

視圖模型:

public class SOServiceViewModel 
{ 
    public SOServiceViewModel() 
    { 
     _Services = new List<SOService>(); 

     _Services.Add(new SOService() 
     { 
      ServiceName = "service1", 
      SOFiles = new List<SOFileInfo>() { new SOFileInfo() { FileName = "File1", FilePath = "c:" } } 
     }); 
    } 
    List<SOService> _Services; 

    public List<SOService> Services 
    { 
     get { return _Services; } 
     set { _Services = value; } 
    } 
} 

和XAML中: enter image description here

回答

0

這裏是我找到了解決辦法:

添加屬性檢索FileNamesFilePaths名單:

public List<string> FileNames 
    { 
     get { return _SOFiles.Select(p => p.FileName).ToList(); } 
     private set { } 
    } 
    public List<string> Filepaths 
    { 
     get { return _SOFiles.Select(p => p.FilePath).ToList(); } 
     private set { } 
    } 

然後在XAML文件:

<GridViewColumn Header="File Name" Width="100"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate DataType="{x:Type local:SOFileInfo}"> 
         <ItemsControl ItemsSource="{Binding FileNames}" ></ItemsControl> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn Header="File Path" Width="100"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate DataType="{x:Type local:SOFileInfo}"> 
         <ItemsControl ItemsSource="{Binding Filepaths}" ></ItemsControl> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn>