显示图像的问题

时间:2013-07-10 08:40:20

标签: c# wpf image windows-phone-7 xaml

好的,这是我的问题:

  • 我有一个自定义类,其中包含一个项目列表 - 每个项目都有一个与之关联的图像路径
  • 我在项目中添加了一个包含图像的文件夹(所以我想在XAP中也有添加,是吗?)
  • 当我尝试在XAML中绑定图像项的源时,它无法正常工作。

<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                <Image Height="100" Width="100" Margin="12,0,9,0" Source="/AlbumArt/{Binding AlbumArt}"/>
                <StackPanel Width="311">
                    <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}"/>
                    <TextBlock Text="{Binding Author}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我做错了什么?有什么想法吗?


P.S。

  • 我也试过了Source="{Binding AlbumArt}",但它仍然没有显示任何内容。

  • 最疯狂的是,如果我将Source设置为特定图像(例如/AlbumArt/someImage.jpg),图像似乎在Visual Studio&amp;模拟器。

5 个答案:

答案 0 :(得分:0)

我不认为Binding是这样的。如果无法将字符串"/AlbumArt/"添加到图像路径属性(AlbumArt),我建议使用转换器。

此外,格式化字符串仅在目标属性为字符串时才起作用,因此StringFormat不在。如果我对StringFormat

错了,有人会纠正我

答案 1 :(得分:0)

如果您有绑定问题,请务必查看输出窗口以获取详细信息。这是显示有关绑定错误的信息的地方。

Source="/AlbumArt/{Binding AlbumArt}"无效,因为它只会被视为字符串。

没有看到你的课,很难确定,但你绑定的属性(“AlbumArt”)应该是Uri而不是string,它应该填充相对的Uri到图像。该图像也应设置为具有内容的构建操作。

答案 2 :(得分:0)

你需要的是这个..

<Image Height="100" Width="100" Margin="12,0,9,0" Source="{Binding ImagePath}"/>

正如你所说的那样你有项目列表(我认为它也是具有图像路径的类)所以在这个类中创建这个属性,并且每个项目都将此路径中的路径放在相应的项目中。

private string _ImagePath;
public string ImagePath
{
   get
   {
     return _ImagePath;
   }
   set
   {
    _imagePath = value;

   }
}

如果在项目类中实现INotifyPropertyChanged会更好。

答案 3 :(得分:0)

如何变得更加诚实。

private string _AlbumArt;
public string AlbumArt
{
   get
   {
     return _AlbumArt;
   }
   set
   {
if(_AlbumArt!=null)
    _AlbumArt=@"/AlbumArt/"+ value;

   }
}

和Binding

<Image Height="100" Width="100" Margin="12,0,9,0" Source="{Binding AlbumArt}"/>

答案 4 :(得分:0)

我很确定你刚刚使用了错误的路径,试试这个......

"/ApplicationName;component/AlbumArt/{Binding AlbumArt}"

将ApplicationName部分替换为您的应用程序名称。请务必使用%20

替换其中的空格