WPF绑定:另一个List中List中的值

时间:2012-12-03 12:48:28

标签: c# wpf xaml listview

我正在创建一个由 ServiceName 分组的ListView,但我无法显示 FileName FilePath ,这里是类和Xaml:

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; }
    }
}

ViewModel:

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

1 个答案:

答案 0 :(得分: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>