用文本和图像动态填充 ListView WPF/C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12769580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:42:03  来源:igfitidea点击:

Dynamically populate ListView with text and Images WPF/C#

c#wpfimagelistview

提问by Chief Wiggum

I am getting really frustrated with listview controls in .NET 4.0. I want to add text into the first column in a row and then add an image to the second column, and then text to the 3rd (all in the top row), and then do the same for the next row. However my code just adds text to all three columns in the top row, then an image to the next row, and then text to the next. I can't work out how to specify a row/column index (eg[1,2] for second row third column).

我对 .NET 4.0 中的列表视图控件感到非常沮丧。我想将文本添加到一行的第一列,然后将图像添加到第二列,然后将文本添加到第 3 列(都在顶行),然后对下一行执行相同的操作。但是,我的代码只是将文本添加到顶行的所有三列,然后将图像添加到下一行,然后将文本添加到下一行。我不知道如何指定行/列索引(例如,第二行第三列的 [1,2])。

I know this appears a quite basic query and there is a lot of info out there but I am new to .NET and the the more I read about it the more confused I get :-(

我知道这看起来是一个非常基本的查询,并且有很多信息,但我是 .NET 的新手,我读得越多,我就越困惑:-(

My c# code is:

我的 C# 代码是:

  ListViewItem lstItem1 = new ListViewItem();
      lstItem.Content = "Picture 1";
      lstView.Items.Add(lstItem);
      Image lstImage = new Image();
      ListViewItem lstItem2 = new ListViewItem();
      lstItem2.Source = SrcBmp;
      lstItemImage.Content = lstImage;
      lstView.Items.Add(lstItem2);
  ListViewItem lstItem3 = new ListViewItem();
      lstItem3.Content = "Blah blah";
      lstView.Items.Add(lstItem3);

XAML here:

XAML 在这里:

<ListView Height="412" HorizontalAlignment="Left" Margin="312,49,0,0" Name="lstView"     VerticalAlignment="Top" Width="636" ItemsSource="{Binding}" FontSize="12"> 
                              <ListView.View>
                    <GridView>
                            <GridViewColumn Header="Photo No." Width="50"/>
                            <GridViewColumn Header="Photo" Width="150"/>
                        <GridViewColumn Header="Description" Width="300"/>
                    </GridView>
                    </ListView.View>
               </ListView>

Many thanks in advance.

提前谢谢了。

Chief Wiggum

维格姆酋长

回答by Ravindra Bagale

you have to create a StackPanelfor your Content property and add the Image and text to that StackPanel. e.g.

您必须StackPanel为您的内容属性创建一个并将图像和文本添加到该 StackPanel。例如

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Width="10" Height="10" Stretch="Fill" Source="{Binding Cover}"/>
        <Label Content="{Binding Title}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>


<Grid x:Name="grid">
    <ListView ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Albums}" />
</Grid>

回答by paparazzo

You added 3 rows and got 3 rows. Should not be a surprise.

您添加了 3 行并获得了 3 行。应该不意外。

ListViewItem only has a single content.

ListViewItem 只有一个内容。

You need to have a class or a strut with those two properties.
Then add those objects to a collection like a List and bind the ListView to the collection.
Then in GridViewColumn the binding path is to the name of the property.

您需要有一个具有这两个属性的类或支柱。
然后将这些对象添加到像 List 这样的集合中,并将 ListView 绑定到该集合。
然后在 GridViewColumn 中,绑定路径是属性的名称。

public class Album 
{
    public string Title { get; set; }
    public Image Img  { get; set; }
}