Reputation: 140
I have list of pictures in an ItemsControl block as shown below
<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>
Any time when a new picture should be added to the list, an object is instantiated from the class Avatar and added to the list.
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);
The problem is that when the image is loaded and I try to change its visibility property (by using avatar.ImgVis), seems the change on avatar object is not propagated to the actual image. Why this happens?
Upvotes: 0
Views: 119
Reputation: 33364
Your Avatar
class should implement INotifyPropertyChanged
interface and each time ImgVis
property is changed PropertyChanged
event should be raised. Same thing should apply to all data bound properties that can can change at runtime.
public class Avatar : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private Visibility _imgVis;
public Visibility ImgVis
{
get{ return _imgVis; }
set
{
if (value == _imgVis) return;
_imgVis = value;
NotifyPropertyChanged("ImgVis");
}
}
}
Upvotes: 3
Reputation: 9242
this is because you have not implement the inotifypropertychange on you avatar class so for doinng that just do like this..
public class Assignment : INotifyPropertyChanged
{
private Visibility _ImgVis ;
public Visibility ImgVis
{
get
{
return _ImgVis ;
}
set
{
_ImgVis = value;
FirePropertyChanged("ImgVis ");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
it will always update whatever changes you will made to your avatar propeties ..if you want to the changes on avatar on add and del...either make it a observable collection
Upvotes: 1