2013-03-21 63 views
0

我遇到了一個問題,即在顯示列表框綁定文本時,沒有任何綁定圖像。我下載並解析一個xml文件就好了,顯示我想要的文本,但是想根據狀態顯示一個圖像。 LinenameService顯示OK,但綁定圖像根本不顯示。 Atype只是用來調用GetImage方法(不是我知道的)。它應該根據狀態設置ImageSource,但根本不顯示圖像。綁定後圖像不顯示在列表框中

XElement XmlTweet = XElement.Parse(e.Result); 
var ns = XmlTweet.GetDefaultNamespace(); 

listBox1.ItemsSource = from tweet in XmlTweet.Descendants(ns + "LineStatus") 
            select new FlickrData 
    { 

Linename = tweet.Element(ns + "Line").Attribute("Name").Value,          
Service = tweet.Element(ns + "Status").Attribute("Description").Value, 
Atype = GetImage(tweet.Element(ns + "Status").Attribute("Description").Value) 

    }; 


    public String GetImage(String type) 
    { 
     FlickrData f = new FlickrData(); 
     switch(type) 
    { 

     case "Good Service": 
      f.Type = new BitmapImage(new Uri("/Images/status_good.png", UriKind.Relative)); 
      break; 
     case "Minor Delays": 
      f.Type = new BitmapImage(new Uri("/Images/status_minor.png", UriKind.Relative)); 
      break; 
     case "Severe Delays": 
      f.Type = new BitmapImage(new Uri("/Images/status_severe.png", UriKind.Relative)); 
      break; 
     case "Planned Closure": 
      f.Type = new BitmapImage(new Uri("/Images/status_minor.png", UriKind.Relative)); 
      break; 
     } 
     return "anything"; 
    } 

在FlickrData這是一個簡單的獲取設置與Type不顯示的ImageSource。

public class FlickrData 


    { 
     public string Linename { get; set; } 
     public string Service { get; set; } 
     public string Detail { get; set; } 
     public ImageSource Type { get; set; } 
     public string Atype { get; set; } 

    } 
+0

你是如何設置綁定在用戶界面? – 2013-03-21 15:01:28

回答

1

轉換器在這種情況下派上用場。

首先,你在XAML形象應該像這樣

<Image Source="{Binding Path=Atype, Converter={StaticResource AtypeToImageConverter}}" Width="100" Height="100"/> 

定義然後在項目中創建一個轉換器類。 (右鍵點擊項目名稱 - >選擇添加 - >選擇類)

的名稱,該類稱爲「AtypeToImageConverter」

public class AtypeToImageConverter: IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (targetType != typeof(ImageSource)) 
      throw new InvalidOperationException("The target must be an ImageSource"); 

     BitmapImage result = null; 
     int type = value.ToString(); 

     switch (type) 
     { 
      case "Good Service": 
       result = new BitmapImage(new Uri("/Images/status_good.png", UriKind.Relative)); 
       break; 

      case "Minor Delays": 
       result = new BitmapImage(new Uri("/Images/status_minor.png", UriKind.Relative)); 
       break; 

      //other cases 
     } 

     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

} 

,它會做的魔力。你可以從你的FlickrData類中刪除Type。 任何疑問,只是谷歌如何使用轉換器在C#

+0

非常感謝您的幫助。得到它的工作。還應該注意在XAML中添加以下內容。 '的xmlns:C = 「CLR-名稱空間:myNameSpace對象」'' <電話:PhoneApplicationPage.Resources> ' – Pete 2013-03-21 16:23:02

+0

我意識到這一點..但希望你學習;) – nkchandra 2013-03-21 16:34:33