0

我正在创建一个按ServiceName分组的 ListView ,但我无法显示FileNameFilePath,这里是类和 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; }
    }
}

视图模型:

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: 在此处输入图像描述

4

1 回答 1

0

这是我找到的解决方案:

添加属性以检索 和 的FileNames列表FilePaths

    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>
于 2012-12-03T13:38:00.030 回答