2013-07-10 151 views
0

OK,這是我的問題:問題與顯示圖像

  • 我有一個自定義類,與項目的列表 - 每個都有一個與之關聯的圖像路徑
  • 我添加一個文件夾與項目內的圖像(所以我想也有在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 & Emulator中正常工作。

回答

0

我不認爲綁定是這樣工作的。如果無法將字符串"/AlbumArt/"添加到圖像路徑屬性(即AlbumArt),我建議使用轉換器來完成此操作。

另外,格式化字符串僅在目標屬性是字符串時才起作用,因此StringFormat不存在。有人糾正我,如果我錯了StringFormat

0

如果你有綁定的問題總是檢查輸出窗口的細節。這是顯示有關綁定錯誤的信息的地方。

這個Source="/AlbumArt/{Binding AlbumArt}"將不起作用,因爲它只會被視爲一個字符串。

沒有看到你的班級,很難確定,但你綁定到的屬性(「AlbumArt」)應該是Uri而不是string,它應該用相對URI填充圖像。該圖像還應該設置爲具有內容的構建操作。

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; 

    } 
} 

如果您在item類中實現INotifyPropertyChanged會更好。

0

如何獲得更多的肛門。

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

    } 
} 

和綁定

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

我敢肯定你剛剛使用了錯誤的道路,試試這個....

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

更換應用程序名稱段與您的應用程序名稱當然。請確保將其中的空間更換爲%20