多行WPF ListViewItem

时间:2018-02-12 08:40:37

标签: wpf listview

我有一个ListView我需要填充一些文本数据。

目前我的ListViewItem看起来像这样(2列):

Name     Number of files    Files finished

XAML:

<ListView.View>
    <GridView>


    </GridView>
</ListView.View>

此处我有GridViewColumnHeaderBindingName数据,例如<!-- file name column --> <GridViewColumn> <GridViewColumnHeader Content="File name" /> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> <GridViewColumn> 属性:

Name

我现在要做的是在第一行看到我的NumberOfFiles,在第二行看到FilesFinished <ListView.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <StackPanel> <TextBlock Text="{Binding Name}" /> </StackPanel> <StackPanel Grid.Column="1"> <TextBlock Text="{Binding NumberOfFiles}" /> <TextBlock Text="{Binding FilesFinished}" /> </StackPanel> </Grid> </DataTemplate> </ListView.ItemTemplate>

因此,在快速搜索后,我找到了this解决方案。

所以我补充一下:

ListView

因此,在将我的文件添加到ListView.View后,我看不到任何文件,因此我尝试删除int(input('something...')),但仍然缺少文件。

我错了什么?

1 个答案:

答案 0 :(得分:0)

看起来很好。我只是添加listview名称。

<Grid>
    <ListView Name="ListViewFiles">
        <ListView.View>
            <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors">
                <GridViewColumn Header="File name" Width="120" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}"/>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="NumberOfFiles" Width="120" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding NumberOfFiles}"/>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

代码示例。也许你忘了ListViewFiles.ItemsSource = items;

    public MainWindow()
    {
        InitializeComponent();
        List<File> items = new List<File>();
        items.Add(new File() { Name = "File One", NumberOfFiles = 2, Filesfished = "Yes" });
        items.Add(new File() { Name = "File Two", NumberOfFiles = 2, Filesfished = "No" });
        ListViewFiles.ItemsSource = items;
    }

    public class File
    {
        public string Name { get; set; }

        public int NumberOfFiles { get; set; }

        public string Filesfished { get; set; }
    }