【问题标题】:C# Listview adding item with image and text, and align the text to leftC# Listview 添加带有图像和文本的项目,并将文本左对齐
【发布时间】:2013-06-17 15:54:24
【问题描述】:

我正在尝试创建一些测试程序,只是为了学习 c# 的乐趣,但我发现了一些我真的想不通的东西。

我想将图像添加到列表视图中的项目。我在 Stackoverflow 上找到了一篇文章,解释了如何做到这一点,并且成功了。但是,我无法向该项目添加额外的文本。我想要一张旁边有文字的图片。我当前的代码:

ImageList Imagelist = new ImageList();
private void Form1_Load(object sender, EventArgs e)
    {
        //retrieve all image files
        String[] ImageFiles = Directory.GetFiles(@"C:\test");
        foreach (var file in ImageFiles)
        {
            //Add images to Imagelist
            Imagelist.Images.Add(Image.FromFile(file));
        }
        //set the amall and large ImageList properties of listview
        listView1.LargeImageList = Imagelist;
        listView1.SmallImageList = Imagelist;

        listView1.Items.Add(new ListViewItem() { ImageIndex = 0});
    }

显然它只会添加一张图片,这是本意。无论如何,我将如何在图像旁边输入文字?例如

listView1.Items.Add(new ListViewItem() { ImageIndex = 0} "Image 1");

文字必须位于图片后面。

我还有第二个问题。 我没有列(添加它们也不起作用)。我想让项目与 ListView 的左侧对齐。我怎么能这样做?

谢谢!

【问题讨论】:

  • 简答:切换到 WPF。使用DataTemplate 可以在 10 分钟内完成。
  • @programmer93 - 谢谢。但是,我不确定我是否能够通过 XAML 使这个动态化。最终我将使用 MySQL 并让脚本根据结果添加一个列表项。
  • 您不必在 XAML 中执行此操作。您仍然可以使用代码隐藏。无论如何,是的,这可以在 XAML 以及代码隐藏中完成。

标签: c# image text listitem


【解决方案1】:

这应该可以解决您的问题。

listView1.View = View.Details; // Enables Details view so you can see columns
listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Image 1" }); // Using object initializer to add the text

【讨论】:

  • 谢谢Zicore!它解决了我的问题。不过,有一个问题我想问。是否可以锁定列以使其无法调整大小?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
相关资源
最近更新 更多