I would follow @Depechie comment and use special property for this. I would also extend your Article
class a little to notify UI when there is a chance that ImagePath[0]
might have changed. I think it could look like this:
public class Article : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> ImagePath = new ObservableCollection<string>();
private string subject;
public string Subject
{
get { return subject; }
set { subject = value; RaiseProperty("Subject"); }
}
private string words;
public string Words
{
get { return words; }
set { words = value; RaiseProperty("Words"); }
}
public Article()
{
ImagePath.CollectionChanged += (sender, e) => { RaiseProperty("FirstImage"); };
}
// public Uri FirstImage // this can work if your image is a content of your App
// {
// get
// {
// return new Uri(ImagePath.FirstOrDefault(), UriKind.Relative);
// }
// }
public BitmapImage FirstImage // getter - BitmapImage
{
get
{
if (String.IsNullOrEmpty(ImagePath[0])) return null;
BitmapImage temp = new BitmapImage();
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream file = ISF.OpenFile(ImagePath[0], FileMode.Open, FileAccess.Read))
temp.SetSource(file);
return temp;
}
}
public void RaiseProperty(string property = null)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
As you can see I've changed your List
to ObservableCollection
, and thus I'm able to subscribe in constructor to CollectionChanged
event. So when the ImagePath
changes, PropertyChanged
is raised and your Image
can be updated.
I've also created mentioned property FirstImage
with get
accessor, so that your Image
can get Uri from it. Usage:
<Image Source="{Binding FirstImage}" Height="50" Width="50" Grid.Row="0"
Grid.Column="0" VerticalAlignment="Top" Margin="0,7,7,0" Grid.RowSpan="2">
I've also made your Subject
and Words
properties and added them a chance to update UI when they have changed.
And last but maybe the most important thing - getting an image depends on where it is - if it was downloaded and is an IsolatedStorageFile
or it is a build in content. I've edited my answer to set it from IsolatedStorage, as I suspect, that you have your images there. If they are a build in content then uncomment lines above.