图像未显示在网格中

时间:2011-07-20 20:55:34

标签: image data-binding windows-phone-7 listbox

在我的应用程序中,我想在列表框中显示一个图片,该列表框通过数据绑定链接到对象。 但是,图片由于某种原因没有出现,我似乎无法发现错误。 我知道图片在对象中,因为如果我将新的Image对象添加到XAML中,并且在代码中将其源设置为来自对象的其中一个图像,则会显示它。

以下是我的代码步骤:

        foreach (Indtastning indt in listBoxIndhold.ItemsSource)
        {
            byte[] data = Convert.FromBase64String(indt.imageName);
            Stream memStream = new MemoryStream(data);
            WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream);
            indt.picture = new Image();
            indt.picture.Source = wbimg;
            //Below is my test image, which shows the picture correctly. 
            testimage.Source = indt.picture.Source;
        }

我的XAML与图像:

<ListBox x:Name="listBoxIndhold" Grid.Row="0"  
                     ItemsSource="{Binding .}"
                     ScrollViewer.VerticalScrollBarVisibility="Visible"   >
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="False">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>

                        </Grid.ColumnDefinitions>

                        <TextBlock Text="{Binding Mode=OneWay, Path=name}" Grid.Column="0"
                                       HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                       Style="{StaticResource PhoneTextSmallStyle}"
                                       TextWrapping="Wrap" FontSize="24"> 
                               <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu>
                                        <toolkit:MenuItem Tag="{Binding Mode=OneWay, Path=name}" Header="Rediger"  Click="MenuItem_Click" />
                                        <toolkit:MenuItem Tag="{Binding Mode=OneWay, Path=name}" Header="Slet"  Click="MenuItem_Click_1"/>
                                    </toolkit:ContextMenu>
                               </toolkit:ContextMenuService.ContextMenu>
                        </TextBlock>

                        <TextBlock Text="{Binding Mode=OneWay, Path=description}" Grid.Column="1"
                                       HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                       Style="{StaticResource PhoneTextSmallStyle}" TextWrapping="Wrap"
                                        FontSize="24"> 
                               <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu>
                                        <toolkit:MenuItem Tag="{Binding Mode=OneWay, Path=name}" Header="Rediger"  Click="MenuItem_Click" />
                                        <toolkit:MenuItem Tag="{Binding Mode=OneWay, Path=name}" Header="Slet"  Click="MenuItem_Click_1"/>
                                    </toolkit:ContextMenu>
                               </toolkit:ContextMenuService.ContextMenu>
                        </TextBlock>

                        <Image Source="{Binding Mode=OneWay, Path=picture}" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" Width="48" Height="48" />

                        <TextBlock Text="{Binding Mode=OneWay, Path=amount}" Grid.Column="3"
                                       HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                       Style="{StaticResource PhoneTextSmallStyle}"
                                       FontSize="24" > 
                               <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu>
                                        <toolkit:MenuItem Tag="{Binding Mode=OneWay, Path=name}" Header="Rediger"  Click="MenuItem_Click" />
                                        <toolkit:MenuItem Tag="{Binding Mode=OneWay, Path=name}" Header="Slet"  Click="MenuItem_Click_1"/>
                                    </toolkit:ContextMenu>
                               </toolkit:ContextMenuService.ContextMenu>
                        </TextBlock>

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我的入职课程:

[DataContract]
public class Indtastning
{
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public double amount { get; set; }
    [DataMember]
    public string description { get; set; }
    [DataMember]
    public bool owes { get; set; }
    [DataMember]
    public string id { get; set; }
    [DataMember]
    public string imageName;

    public Image picture;


    //Constructor
    public Indtastning(string id , string navn, double beløb, string beskrivelse, bool skylder)
    {
        this.name = navn;
        this.description = beskrivelse;
        this.amount = beløb;
        this.owes = skylder;
        this.id = id;
    }
}

有谁能发现我在这里做错了什么? 这真的很烦人,因为它是我在启动应用程序之前需要排序的最后一件事。

2 个答案:

答案 0 :(得分:0)

您是否已将图片保存为内容或资源。如果图像是资源,您将看不到它,将其设置为内容并重试。希望它可以提供帮助

答案 1 :(得分:0)

很久以前问题就解决了。 请参阅上面的评论。