0

我有一个 Windows Phone 应用程序,它使用 WCF 从 SQL 服务器获取数据。数据进入一个 ListBox 并且有一个名为 Status 的列。列状态,需要显示图像而不是数字,知道我显示 0 表示成功,1 表示警告,2 表示警报。谁能指出我正确的方向?谢谢。

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using NSSPhoneApp.NSSServiceReference;
using System.ServiceModel;

namespace NSSPhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            NSSServiceClient client = new NSSServiceClient();
            client.GetAllServer_LogsCompleted +=
                new EventHandler<GetAllServer_LogsCompletedEventArgs>(client_GetAllServer_LogsCompleted);
            client.GetAllServer_LogsAsync();
        }

        void client_GetAllServer_LogsCompleted(object sender, GetAllServer_LogsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                lst.ItemsSource = e.Result;
            }
        }
    }
}

MainPage.xaml 仅 ContentPanel

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,0,6" Name="lst" VerticalAlignment="Stretch" Width="Auto">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Status}" Margin="12,0,12,0"/>
                        <TextBlock Text="{Binding Name}" Margin="12,0,12,0"/>
                        <TextBlock Text="{Binding IP}" Margin="12,0,12,0"/>
                        <TextBlock Text="{Binding Task}" Margin="12,0,12,0"/>
                        <TextBlock Text="{Binding Message}" Margin="12,0,12,0"/>
                        <TextBlock Text="{Binding Time}" Margin="12,0,12,0"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>
4

1 回答 1

0

未经测试,您将不得不弄清楚如何正确显示图像。

listBox.Controls.Add(new Label() { Text = "Status: ", Image = System.Drawing.Image.FromFile(status == "0" ? "succes.png" : status == "1" ? "warning.png" : "alarm.png") });
于 2012-08-21T10:42:36.093 回答