如何在wp7中动态绑定列表框中的图像源?

时间:2012-06-11 09:05:32

标签: windows-phone-7

mycode的:

<ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Image Height="100" Width="105" Source="{Binding Icon}" VerticalAlignment="Top" OpacityMask="White" Stretch="Fill" />
                                <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextSubtleStyle}"
                              Width="110" TextAlignment="Center"/>
                                <Image  Source="{Binding ImageSrc}" Height="20" Width="20"  VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-50,15,0" Stretch="Fill"  />

                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>



    interestrates = (from rts in xmlDocu.Descendants("Friend")

                                     select new SampleCheckedData
                                     {

                                         Id = (string)rts.Element("userid"),
                                         Name = (string)rts.Element("name"),
                                         Icon = (string)rts.Element("imageurl"),
                                         VisibleStatus = (string)rts.Element("visiblestatus"),
                                         AppStatus = (string)rts.Element("loginstatus"),

                                     }).ToList<SampleCheckedData>();



                    this.lstImages.ItemsSource = interestrates;

我能够正确绑定Name和Icon。但是我需要将imagesrc图像设置为visiblestatus,Appstatus booth是真的然后需要绑定一种类型的图像,否则需要绑定另一种类型的图像。如何实现这个?

2 个答案:

答案 0 :(得分:1)

只需使用名为ImageSrc的属性,如下所示:

public class SampleCheckedData
{

//... your properties

        public Uri ImageSrc
        {
            get
            {
               if ((bool.Parse(this.VisibleStatus) && (bool.Parse(this.AppStatus))
                {
                if (!string.IsNullOrEmpty(this.Icon))
                    return new Uri(this.Icon, UriKind.Absolute); //or whatever image
                }
                else
                {
                    return new Uri("/Images/YourOtherImage.png", UriKind.Relative);
                }

            }
        }
}

它应该工作。至少这是我用来绑定图像的方式......

注意:我假设Icon属性具有图像的绝对路径。

答案 1 :(得分:0)

只是添加到@ josemiguel.torres,image.Source是ImageSource类型,所以你不想返回Uri,而是想要这样做:

return new BitmapImage(new Uri(this.Icon, UriKind.Absolute));