我在 ItemsControl 块中有图片列表,如下所示
<ItemsControl Name="icAvatars">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Grid>
<TextBlock Visibility="{Binding LdVis}" Text="Loading... "/>
<TextBlock Visibility="{Binding ErrVis}" Text="Error while loading the image."/>
<Image Source="{Binding ImgSrc}" Visibility="{Binding ImgVis}"/>
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
每当应将新图片添加到列表中时,都会从 Avatar 类实例化一个对象并将其添加到列表中。
public class Avatar
{
public BitmapImage ImgSrc { get; set; }
public Visibility LdVis { get; set; }
public Visibility ImgVis { get; set; }
public Visibility ErrVis { get; set; }
}
Avatar avatar = new Avatar();
var bitmap = new BitmapImage(new Uri(uri));
bitmap.ImageOpened += (s, e) => avatar.ShowImage();
avatar.ImgSrc = bitmap;
icAvatars.Items.Add(avatar);
问题是,当图像被加载并且我尝试更改其可见性属性(通过使用 avatar.ImgVis)时,似乎化身对象的更改不会传播到实际图像。为什么会发生这种情况?