I have an ObservableCollection, and this is MyObject definition:
public class MyObject : INotifyPropertyChanged
{
private bool favourite;
public event PropertyChangedEventHandler PropertyChanged;
public b开发者_运维百科ool Favourite
{
get
{
return favourite;
}
set
{
favourite = value;
var eh = PropertyChanged;
if (eh != null)
{
eh(this, new PropertyChangedEventArgs("Favourite"));
}
}
}
public bool Name { get; set; }
}
I want to bind this collection to a WP7 listbox, but only the elements that have the property Favourite = true. How should I bind the listbox to the collection? have in consideration that ObservableCollection, that is a global variable, receives changes (Favourite value) from other parts of the application.
I tried this without success (when the favourite property of one element changes, the listbox is not refreshed):
ObservableCollection<Channel> myChannels =
(((App)Application.Current).FavouriteChannels
.Where(f=>f.Favorito == true)
.OrderBy(o => o.SortIndex)
.ToList()).ToObservableCollection<Channel>();
this.listFavoritos.ItemsSource = myChannels;
Any ideas?
In the last line you should be setting the ItemSource of ListBox to myChannels.
this.listFavoritos.ItemSource = myChannels;
The simplest solution is to create a seaparate collection which just contains the items you want.
Alternatively, you could use a CollectionViewSource.
精彩评论