I am getting some data from an xml that updates every 2 minutes or so with async WebRequest. So i need everytime the data changes the listbox to change accordingly. I pull the data from the internet and the last lines of code are these.
IEnumerable<Update> list = from y in xelement.Descendants("Song")
select new Update()
{
NowTitle = y.Attribute("title").Value,
NowArtist = y.Element("Artist").Attribute("name").Value
};
Dispatcher.BeginInvoke(()=> nowList.ItemsSource = list);
XAML looks like this.
<ListBox x:Name="nowList" Height="86" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,0" Orientation="Vertical" Background="Gray" HorizontalAlignment="Stretch">
<TextBlock Height="Auto" Width="480" Text="{Binding Path=NowTitle, Mode=OneWay}" TextWrapping="Wrap" TextAlignment="Center" FontSize="24" FontWeight="Bold" Foreground="#FFE5D623" HorizontalAlignment="Stretch" />
<TextBlock Height="Auto" Width="480" Text="{Binding Path=NowArtist, Mode=OneWay}" TextWrapping="Wrap" TextAlignment="Center" FontSize="24" FontWeight="Bold" Foreground="#FFE5D623" HorizontalAlignment="Stretch" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
the class Update that contains the properties is this.
public class Update : INotifyPropertyChanged
{
string nowTitle;
string nowArtist;
public string NowTitle
{
get
{
if (!string.IsNullOrEmpty(nowTitle))
{
return "Τώρα : " + nowTitle;
}
else
{
return "something";
}
}
set { this.nowTitle = value;
NotifyPropertyChanged("NowTitle");
}
}
public string NowArtist
{
get
{
if (!string.IsNullOrEmpty(nowTitle))
{
return "by " + nowArtist;
}
else
{
return "";
}
}
set { this.nowArtist = value;
NotifyPropertyChanged("NowArtist");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Can anyone tell me what am i doing wrong? thanks!
two things, first, make sure your
nowListproperty is raising property changed events, second, make sure yournowListis of typeObservableCollection< Update >If nowList is your listbox, that is more then likely your culprite. try making an
ObservableCollection<Update>as a property that raises change events, and then in your XAML bind your list box to that…I am relatively sure that will fix your problem