ListBox不显示图像

时间:2011-12-12 11:45:53

标签: silverlight image xaml windows-phone-7 binding

我有一个列表框XAML,看起来像这样

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <ListBox HorizontalAlignment="Left" Margin="12,12,0,0" Name="ComplaintslistBox" VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Width="400" BorderThickness="1">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <StackPanel>
                                <Image Source="{Binding Type}" Width="30" Height="30"></Image>
                            </StackPanel>
                            <StackPanel>
                                <TextBlock Text="{Binding Head}" Width="300"></TextBlock>
                            </StackPanel>
                        </StackPanel>
                    </Button.Content>
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

现在的问题是它绑定了“Head”但没有绑定“Type”

类型变量包含要显示的图像的路径....

类看起来像这样

public class Complaints
{
    public string Id { get; set; }
    public string Type { get; set; }
    public string Head { get; set; }
    //public string Desc { get; set; }
    public string lat { get; set; }
    public string lon { get; set; }
    public string wardNo { get; set; }
    public string wardName { get; set; }
    public string creator { get; set; }
    public string url { get; set; }
    public string catId { get; set; }
    public string subCatId { get; set; }
    public int commentCount { get; set; }
    public int solutionCount { get; set; }
    public int affectedCount { get; set; }

    public Complaints() { }

    public Complaints(Complaints complaints)
    {
        this.Id = complaints.Id;
        this.Head = complaints.Head;
        //this.Desc = complaints.Desc;

        switch (complaints.Type)
        {
            case "Reported":
                this.Type = "icons/icon_red.png";
                break;
            case "Reopened":
                this.Type = "icons/icon_black.png";
                break;
            case "Resolved":
                this.Type = "icons/icon_green.png";
                break;
            default:
                this.Type = "icons/icon_blue.png";
                break;
        }

        this.wardNo = complaints.wardNo;
        this.wardName = complaints.wardName;

        this.lat = complaints.lat;
        this.lon = complaints.lon;

        this.catId = complaints.catId;
        this.subCatId = complaints.subCatId;

        this.url = complaints.url;
        this.creator = complaints.creator;

        this.affectedCount = complaints.affectedCount;
        this.commentCount = complaints.commentCount;
        this.solutionCount = complaints.solutionCount;
    }
}

由于

2 个答案:

答案 0 :(得分:1)

绑定到ImageSource可能比绑定到string更容易。

你需要这样的东西:

var ISC = new System.Windows.Media.ImageSourceConverter();

this.Type = (ImageSource)ISC.ConvertFromString(ImagePath);

其中Type为:

public ImageSource Type { get; set; }

而不是string

答案 1 :(得分:0)

问题在于这些图像的位置,如果这些是程序集中的资源,您需要使用Pack URI语法限定路径,事实上,完全限定路径绝对不会受到伤害。因此,如果您使用资源,它应该是:

"pack://application:,,,/icons/icon_red.png"