2013-03-21 64 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