将ListmapImage绑定到ListBox上的Image不起作用

时间:2015-06-09 13:01:20

标签: c# wpf silverlight listbox

我正在开发一个Windows Phone 8.1 Silverlight项目 我在XAML文件上有一个ListBox,我想像WP 8.1中的联系人一样显示这个ListBox,它有一个图片和联系人名称

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

如您所见,Image属性是从名为Contact的对象绑定的:

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

从文件中读取图像并将其指定给Image属性

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

我试图在Grid中的Image组件上显示它并且它运行良好,但它在ListBox中不起作用。我怎么能解决这个问题,谢谢你们!

0 个答案:

没有答案
相关问题