WPF:将图像添加到ListBox ItemTemplate [英] WPF: Adding an image to a ListBox ItemTemplate

查看:1060
本文介绍了WPF:将图像添加到ListBox ItemTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用绑定到项目名称的列表框创建WPF应用程序.作为装饰元素,我想在列表中的每个项目旁边放置一个小图标,类似于Outlook在其个人文件夹"列表中所做的方式.首先,我将对列表中的所有项目使用相同的图像.

I am creating a WPF app with a list box that I am binding to project names. As a decorative element, I want to place a small icon next to each item in the list, similar to the way Outlook does in its Personal Folders list. For starters, I am going to use the same image for all items in the list.

这是到目前为止的标记(在工作后,我将其移至资源字典中):

Here is the markup that I've got so far (I'll move it to a resource dictionary after it's working):

<ListBox.Resources>
    <ImageBrush x:Key="ProjectIcon" ImageSource="Images/project.png" />
</ListBox.Resources>
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <Image Source="{StaticResource ProjectIcon}"/>
            <TextBlock Text="{Binding Path=Name}" />
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

图像资源有错误,但是我不确定如何解决.有什么建议?谢谢.

I've got an error in the image resource, but I'm not sure how to fix it. Any suggestions? Thanks.

推荐答案

ImageSource属性的类型为ImageSource而不是ImageBrush.以下应该可以工作:

The Source property of an Image is of type ImageSource not ImageBrush. The following should work:

<ListBox.Resources>
    <BitmapImage x:Key="ProjectIcon" UriSource="Images/project.png" />
</ListBox.Resources>
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <Image Source="{StaticResource ProjectIcon}"/>
            <TextBlock Text="{Binding Path=Name}" />
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

这篇关于WPF:将图像添加到ListBox ItemTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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