WP7
WP7

Reputation: 165

How to bind image source in a listbox dynamically in wp7?

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;

I am able to bind the Name and Icon properly.But i need to set Imagesrc image as if visiblestatus,Appstatus booth are true then need to bind one type of image else need to bind another type of image thorugh code.How to acheive this?

Upvotes: 0

Views: 1245

Answers (2)

Jonny Lin
Jonny Lin

Reputation: 777

Just to add on to @josemiguel.torres, image.Source is of the type ImageSource, so instead of returning the Uri, you would want to do this:

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

Upvotes: 0

josemiguel.torres
josemiguel.torres

Reputation: 632

Just use a property called ImageSrc as follow:

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);
                }

            }
        }
}

It should work. At least this is how I use to bind images...

NOTE: I'm assuming Icon property has the absolute path to the image.

Upvotes: 1

Related Questions