0

リストボックスにバインディング テキストが表示されているのに、バインディング イメージが表示されないという問題があります。私はxmlファイルをダウンロードして解析し、必要なテキストを表示しますが、ステータスに応じて画像を表示したいと考えています。LinenameOKをService表示しますが、バインディング イメージはまったく表示されません。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が表示されない単純な get set です。

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

    }
4

1 に答える 1

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# でコンバーターを使用する方法についてググってください。

于 2013-03-21T13:51:52.100 に答える