我的应用程序中有一个图像列表,它们都在一个列表框中,我将源作为URL提供。
一切都很好,我可以显示我给出的URL中的图像。
当我更改图像并重新加载ListBox
时,该更改不会反映在该图像上,它显示的是旧图像,而不是新图像。
我确信,在图像更改后,我将新的图像URL绑定到源,但它显示了旧的图像,我无法找到为什么会发生这种情况,这里是我的代码
<ListBox Grid.Row="1" HorizontalAlignment="Left" Margin="0,81,0,0" Name="wishListListBox" VerticalAlignment="Top" Width="480" Visibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Image ImageFailed="wishlistImage_ImageFailed_1" x:Name="wishlistImage" HorizontalAlignment="Left" Width="164" Margin="12,54,0,88" Stretch="Fill">
<Image.Source>
<BitmapImage CreateOptions="DelayCreation,IgnoreImageCache" UriSource="{Binding thumb_image}" />
</Image.Source>
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我为图像设置项目源的类
[DataContract]
public class WishListResponse : INotifyPropertyChanged
{
[DataMember]
public List<string> thumb_images { get; set; }
public string thumb_image
{
get
{
if (thumb_images.Count != 0)
return thumb_images[0];
else
return "";
}
set
{
thumb_images[0] = value;
OnPropertyChanged(thumb_images[0]);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
在我的主页上,我像这个一样绑定项目源
public static List<WishListResponse> wishlistList = new List<WishListResponse>();
wishListListBox.ItemsSource = wishlistList.ToArray();
有人能帮我刷新图像控制吗。非常感谢。
好的,感谢您的更新:-)
第一个问题..;-)如果将List
替换为ObservableCollection
,则当您更改列表中的条目时,它将自动更新UI。
public ObservableCollection<string> ThumbImages { get; set; }
现在,如果您将ThumbImages设置为新的,即为其分配一个新对象,则必须将DataContext再次设置为新引用,例如:
ThumbImages = new ObservableCollection<string>(wishListResponse.thumb_images);
DataContext = ThumbImages;
现在,通常将datacontext设置为视图的模型,在您的情况下,它是一个字符串列表。您可以在代码背后设置这样的数据上下文:
DataContext = ThumbImages;
然后,您可以使用默认绑定引用数据上下文:
<ListBox ItemsSource="{Binding}">...</ListBox>
因此,您将不再需要在WishListResponse上创建一个特殊属性,并将ItemTemplate再次设置为Element,即在这种情况下,再次使用默认绑定:
<ListBox ItemsSource="{Binding}" Grid.Row="1" HorizontalAlignment="Left" Margin="0,81,0,0" Name="wishListListBox" VerticalAlignment="Top" Width="480" Visibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Image ImageFailed="wishlistImage_ImageFailed_1" x:Name="wishlistImage" HorizontalAlignment="Left" Width="164" Margin="12,54,0,88" Stretch="Fill">
<Image.Source>
<BitmapImage CreateOptions="DelayCreation,IgnoreImageCache" UriSource="{Binding}" />
</Image.Source>
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
HTH
我在这个问题上尝试了非常简单的解决方案,它对我有效,
我只是每次需要刷新图像时都更改了图像链接,这意味着我只需将当前时间添加到图像url中。
这对我来说很好。
谢谢。
我认为代码中的更改不会通知给xaml,因此您必须从INotifyChange继承该类,然后进行属性更改事件。。。像
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
则在之后的列表的属性集合中
listname = value;
OnPropertyChanged("listname");
我希望这能对你有所帮助。。。。
我认为原因是你的模型没有实现INotifyPropertyChanged,所以你的属性已经更改,但无法通知图像绑定属性,以解决这个
一种是实现INotifyPropertyChanged;
另一个你可以做的像这个
wishListListBox.ItemsSource =null;
wishListListBox.ItemsSource = itemsSource;
它还使您的列表框显示新项目
但当项目源更改时会出现问题,您必须这样做。。。。。。。。
希望这能帮助你