简体   繁体   English

当列表框中的选择更改时更新图像-WPF

[英]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. 我有一个称为GetImageLink的方法,该方法为当前选择获取一个Web URL,并将其分配给ReferenceImageLink属性。 (I checked, link is updated when selection changes). (我检查过,选择更改时链接也会更新)。 But when I try to use this as a source for the Image it does nothing. 但是,当我尝试使用它作为Image的源时,它什么也没做。 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. 我是WPF的新手,所以对我来说有点困惑。

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 WPF应用程序的屏幕截图

making UriKind.Absolute solved my problem. 使UriKind.Absolute解决了我的问题。 Thanks 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM