当前位置: 首页 > 知识库问答 >
问题:

将图像添加到ListBox项目

徐秋月
2023-03-14

我目前正在开发一个C#WPF应用程序,我试图在其中添加一个图像,然后在每个列表项中添加一些文本。

我已经为文本工作的绑定,但图像不显示。

下面是我的XAML:

<Window x:Class="ServerAdministrator.FtpDirectoryListing"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ServerAdministrator"
        Title="FTP Directory Listing" Height="391" Width="599">
    <Grid>
        <StatusBar Height="30" Margin="0,322,0,0" Name="statusBar1" VerticalAlignment="Top" />
        <ToolBar Height="26" Name="toolBar1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,32,0,0" Name="textBox1" VerticalAlignment="Top" Width="517" />
        <ListBox x:Name="lstDirecotryListing" Height="233" HorizontalAlignment="Left" Margin="12,61,0,0" VerticalAlignment="Top" Width="553">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:DirectoryListing}">
                    <StackPanel>
                        <TextBlock Margin="3" Text="{Binding path}" />
                        <ContentControl Margin="3" Content="{Binding image}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

下面是我的Directory清单类

class DirectoryListing
    {
        public string path {get; set;}
        public Image image{get; set;}
        public DirectoryListing(Image imgage, String path)
        {
            this.image = image;
            this.path = path;
        }
    }

下面是我如何添加项目到列表框

Image image = new Image();
            BitmapImage bi = new BitmapImage(new Uri(@"C:\Users\Chris\Documents\Visual Studio 2010\Projects\ServerAdministrator\ServerAdministrator\bin\Debug\images\directory.png"));
            image.Source = bi;
            lstDirecotryListing.Items.Add(new DirectoryListing(image, "hello"));

文字添加得很好,但图像没有。

我不确定这是否相关,但我在VS2010的控制台输出中看到以下内容

系统。Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,ancestor type =”System的绑定的源。Windows.Controls.ItemsControl ',AncestorLevel='1 ' '。binding expression:Path = horizontal content alignment;DataItem = null目标元素是“ComboBoxItem”(名称=“”);目标属性是“HorizontalContentAlignment”(类型为“HorizontalAlignment”)系统。Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,ancestor type =”System的绑定的源。Windows.Controls.ItemsControl ',AncestorLevel='1 ' '。binding expression:Path = VerticalContentAlignment;DataItem = null目标元素是“ComboBoxItem”(名称=“”);目标属性为“VerticalContentAlignment”(类型为“VerticalAlignment”)

感谢您提供的帮助

更新

感谢克莱门斯的回答,我已经让它工作了,仍然使用相同的两个变量,因为路径不是图像的路径,但无论如何,它现在正在显示图像和文本。

问题是它显示文本,图片在下面,我需要并排显示图片和文本,我该怎么做?

共有1个答案

长孙阳成
2023-03-14

将您的视图模型简化为:

public class DirectoryListing
{
    public string Name { get; set; }
    public string Path { get; set; }
}

并将您的数据模板更改为以下内容:

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock Margin="3" Text="{Binding Name}"/>
            <Image Margin="3" Source="{Binding Path}"/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

内置类型转换将自动从文件路径字符串创建图像源。

 类似资料:
  • 嗨,我是android的业余爱好者。我想将图像添加到我的微调器项目中。不幸的是,我不知道怎么做。下面是我的xml文件和Mainactive。感谢阅读。 XML文件 维护活动

  • 问题内容: 此处已触及该主题,但未提供有关如何创建3D图并在平面中以指定高度插入图像的指示。 因此,要提出一个简单且可复制的案例,假设我使用以下代码创建了一个3D图: 在视觉上,我们有: 在级别上,这里是避免重叠的视觉偏移, 我想插入一张图像, 表示曲线显示特定值的元素。 怎么做? 在此示例中,我并不关心元素与其值之间的完美匹配,因此请随时上传您喜欢的任何图像。另外,如果对匹配不满意,有没有办法让

  • 问题内容: 最近8个小时我一直在阅读文档,但没有发现任何可以帮助我的东西。大概是,但是没有代码在工作,因为它一直说“找不到图像URL”并引发异常。但是我还有其他项目,从来没有这个问题。 因此,有一个类包含这样的月份: 到目前为止,一切都很好。我什至可以在控制台中对其进行测试,并且效果很好,并且可以按值排序。现在,当我尝试从资源中添加图像时,出现了我之前提到的问题:找不到URL。但是,我只能使用图像

  • 我正在使用Python FastAPI编写一些Rest API。需要在Swagger-OpenAPI文档中添加许多自定义部分。这包括每个API的联系信息字段、作者信息、显示数据模型的图像、数据库字段等。 有没有办法使用快速API内置的OpenAPI功能来完成所有这些工作?我知道可以托管我们自己的自定义 json 文件。但是我不知道如何将图像添加到招摇的json文件中。我也不知道如何将自定义字段添加

  • 问题内容: 我已经阅读了很多SO和Google链接。 我还没有弄清楚如何正确地将图像添加到eclipse gui项目中,这样系统可以识别找到它。我知道有一些关于CLASSPATH的小技巧,但做起来可能并不难。 让我先描述一下我在做什么…(如果有人可以纠正我,我将不胜感激。) 这是我的方法。 我使用“导入向导”(右键单击,“导入”,“常规”,“文件”)将图像添加到我称为“ / resources”的

  • 问题内容: 我有一个JPanel,我想向其中添加即时生成的JPEG和PNG图像。 到目前为止,我在Swing教程中看到的所有示例,特别是在Swing示例中,都使用ImageIcon。 我将这些图像生成为字节数组,它们通常比示例中使用的通用图标大,尺寸为640x480。 使用ImageIcon类在JPanel中显示该大小的图像时是否存在任何(性能或其他)问题? 什么是平常做的呢? 如何不使用Imag