Binding BitmapImage to Image on ListBox doesn't work

200 views Asked by At

I'm working on a Windows Phone 8.1 Silverlight project and I have a ListBox on XAML file, I want to display this ListBox like contacts in WP 8.1 which has a picture and contact name in a row

                <ListBox x:Name="lbContact" Width="Auto" Height="Auto">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="80"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Border Width="40" Height="40" Background="#FF2E2B2B" Grid.Column="0" Margin="0,4,0,0" >
                                    <Image Width="40" Height="40" Source="{Binding Image}" Stretch="Fill"/>
                                </Border>
                                <TextBlock Margin="3,10,0,0" Text="{Binding Name}" Grid.Column="1" FontSize="20"/>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

As you can see, the Image property was binding from an object named Contact:

public class Contact
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Group { get; set; }
        public BitmapImage Image { get; set; }
    }

An image was read from a file and assigned to Image property

using (Stream s = await picFile.OpenStreamForReadAsync())
{
     BitmapImage img = new BitmapImage();
     img.SetSource(s);
     contact.Image = img;
}

I tried to display it on an Image component inside a Grid and it works well, but it doesn't work in ListBox. How can I solve this problem, thanks you guys!

0

There are 0 answers