Abhijeet Sharma
Abhijeet Sharma

Reputation: 11

Updating Image when the selection in ListBox changes - WPF

What I am trying to achieve is change the source of Image when I change the selection in Listbox. I have a method called GetImageLink that fetches a web URL for the current selection and assigns it to ReferenceImageLink property. (I checked, link is updated when selection changes). But when I try to use this as a source for the Image it does nothing. The image is perpetually blank from start. Can someone please point me in the right direction? I am new to WPF so it is kind of a little confusing to me.

public static string ReferenceImageLink { get; set; }

        private async void VariantListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ReferenceImageLink = null;
            var selecteditem = sender as ListBox;
            string item = selecteditem.SelectedItem as string;
            await GetImageLink(item);


            BitmapImage referenceBMP = new BitmapImage();
            referenceBMP.BeginInit();

            //ReferenceImageLink property changes as ListBox selection change, GetImageLink takes care of it.
            referenceBMP.UriSource = new Uri(ReferenceImageLink, UriKind.Relative);
            referenceBMP.CacheOption = BitmapCacheOption.OnLoad;
            referenceBMP.EndInit();
            ReferenceImageBox.Source = referenceBMP;

        }
<Image Name="ReferenceImageBox" HorizontalAlignment="Left" Height="189" Margin="558,51,0,0" VerticalAlignment="Top" Width="230" />

Screenshot of the WPF app

Upvotes: 0

Views: 159

Answers (1)

Abhijeet Sharma
Abhijeet Sharma

Reputation: 11

making UriKind.Absolute solved my problem. Thanks

referenceBMP.UriSource = new Uri(ReferenceImageLink, UriKind.Absolute);

Upvotes: 1

Related Questions