2010-01-17 74 views
5

我正在创建一个WPF应用程序,其中包含一个列表框,我将其绑定到项目名称。作为一个装饰元素,我想在列表中的每个项目旁边放置一个小图标,类似于Outlook在其个人文件夹列表中的做法。对于初学者,我将为列表中的所有项目使用相同的图像。WPF:将图像添加到列表框ItemTemplate

这里是我到目前为止已经得到了标记(我将它的工作后,将其移动到资源字典):

<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> 

我已经得到的图像资源的错误,但我我不知道如何解决它。有什么建议么?谢谢。

回答

11

SourceImage的性质属于ImageSource而不是ImageBrush。以下应该工作:

<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> 
+0

工作完美 - 再次感谢! – 2010-01-17 19:07:21