C# - 如何将图像从文件夹添加到数组并在listview中显示? [英] C# - how to add images from folder to array and display them in listview?

查看:458
本文介绍了C# - 如何将图像从文件夹添加到数组并在listview中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string[] list = Directory.GetFiles(@"Resources/", "*.jpg");
lvDataBinding.Items.Add(list[0]);

所以文件夹资源包含我要添加到数组的几个图像,所以我可以在一种更简单的方法。

So the folder Resources contains several images that I want to add to an array so I can use them in an easier way.

我需要在一个窗口中显示它们(当选择了不同的listviewitem时,每个窗口都显示它们。)

I need to display them in a window (each one when a different listviewitem is selected).

我想知道我是否也可以将它们与ListViewItem名称和描述一起存储在一个类中。所以我可以这样做:

I would like to know if I can also store them in a class alongside the ListViewItem name and description. So I can do like:

Article article1= new Article();
article1.Name = "Article name";
article1.Description = "Long article description etc etc";
article1.Image= images[0];
lvDataBinding.Items.Add(artikel1);

然后上课会是这样的吗?

And then class would be something like this I guess?

public class Article
{
    public string Name{ get; set; }
    public string Description{ get; set; }
    public Image? Image { get; set; }

    public override string ToString()
    {
        return Naziv;
    }
}

PS:我尊重所有关于如何做的建议这是另一种更好的方式。我确定有更好的方法,但我正在做大学任务,我的时间有限,我的老师也建议将对象转移到新的窗口。
提前致谢!

P.S.: I respect all suggestions on how I could do this on another better way. Im sure there are better ways, but I am doing a college assignment and I'm limited on time, also my teacher suggested to "transfer" objects into new windows. Thanks in advance!

推荐答案

您可以使用 string <存储图像的路径/ code> property。请参阅以下示例代码。

You could store the path of the image using string property. Please refer to the following sample code.

代码:

public class Article
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string Path { get; set; }
}
...
string[] list = Directory.GetFiles(@"Resources/", "*.png");
List<Article> items = new List<Article>();
foreach (var path in list)
{
    items.Add(new Article()
    {
        Name = System.IO.Path.GetFileNameWithoutExtension(path),
        Path = path
    });
}
lvDataBinding.ItemsSource = items;

XAML:

<ListView x:Name="lvDataBinding" DisplayMemberPath="Name" />
<Image Source="{Binding SelectedItem.Path, ElementName=lvDataBinding}" Stretch="None" />

这篇关于C# - 如何将图像从文件夹添加到数组并在listview中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆