我有一个ObservableCollection,这是MyObject定义:
public class MyObject : INotifyPropertyChanged
{
private bool favourite;
public event PropertyChangedEventHandler PropertyChanged;
public bool Favourite
{
get
{
return favourite;
}
set
{
favourite = value;
var eh = PropertyChanged;
if (eh != null)
{
eh(this, new PropertyChangedEventArgs("Favourite"));
}
}
}
public bool Name { get; set; }
}
我想将此集合绑定到WP7列表框,但只包含具有属性Favorite = true的元素。我应该如何将列表框绑定到集合中?考虑到ObservableCollection,即全局变量,从应用程序的其他部分接收更改(收藏夹值)。
我尝试了这个没有成功(当一个元素的最喜欢的属性发生变化时,列表框没有刷新):
ObservableCollection<Channel> myChannels =
(((App)Application.Current).FavouriteChannels
.Where(f=>f.Favorito == true)
.OrderBy(o => o.SortIndex)
.ToList()).ToObservableCollection<Channel>();
this.listFavoritos.ItemsSource = myChannels;
有什么想法吗?
答案 0 :(得分:3)
在最后一行中,您应该将ListBox的ItemSource设置为myChannels。
this.listFavoritos.ItemSource = myChannels;
答案 1 :(得分:1)
最简单的解决方案是创建一个单独的集合,其中只包含您想要的项目。
或者,您可以使用CollectionViewSource。