Windows phone 7 ItemsControl不';在第一次加载页面后,请停止或重新筛选
我正在创建一个带有天气图的应用程序,它以图钉的形式显示不同地方的热量。为此,由支持INotifyPropertyChanged接口的ownWindows phone 7 ItemsControl不';在第一次加载页面后,请停止或重新筛选,windows-phone-7,sorting,filter,observablecollection,inotifypropertychanged,Windows Phone 7,Sorting,Filter,Observablecollection,Inotifypropertychanged,我正在创建一个带有天气图的应用程序,它以图钉的形式显示不同地方的热量。为此,由支持INotifyPropertyChanged接口的ownPushpinModel提供: public class PushpinModel: INotifyPropertyChanged { #region // events public event PropertyChangedEventHandler PropertyChanged; #endregio
PushpinModel
提供:
public class PushpinModel: INotifyPropertyChanged
{
#region // events
public event PropertyChangedEventHandler PropertyChanged;
#endregion events
#region // fields
Heat heat = Heat.normal;
#endregion fields
#region // properties
public string Placename { get; set; }
public GeoCoordinate Location { get; set; }
public Heat Heat
{
get { return heat; }
set
{
if (heat != value)
{
heat = value;
OnPropertyChanged("Heat");
}
}
}
public string IDno { get; set; }
#endregion properties
#region // handlers
protected virtual void OnPropertyChanged(string propChanged)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
}
#endregion handlers
}
PushpinModel
对象包含在名为pushpin的可观察集合中,该集合定期更新以显示天气:
public class Pushpins: ObservableCollection<PushpinModel>
{
#region // METHODS
public void ShowWeather( WeatherReport fromWeatherReport)
{
foreach (WeatherRecord w in fromWeatherReport.WeatherRecords)
{
this.First<PushpinModel>(p => p.IDno == w.PlaceID).Heat = w.Heat;
}
}
#endregion methods
}
其中一个公共枚举热{na,冷,正常,暖,热}
问题是ItemsControl列表在页面加载时显示适当的排序和过滤,但在更改PushpinModel对象属性时不会更新。请注意,当Pushpins对象绑定到Bing地图控件时,PushpinModel对象会按预期进行更新。因此,不知何故,我的ItemsControl列表没有更新,即使它通过CollectionView绑定到ObservableCollection我相信,CollectionViewSource
实现仅在基础集合更改(或重置)时支持自动筛选。如果基础数据项的属性发生更改,则不会调用筛选器
当集合中某个项的属性发生更改时,您可以在CollectionViewSource
上调用Refresh()
,也可以实现自己的CollectionViewSource
,它侦听基础数据对象上的属性更改事件,或者您可以直接绑定到筛选(并排序)的收集而不是使用CollectionViewSource
您正在更改哪些属性?只有您的Heat属性调用PropertyChanged事件,其他属性则不调用。在本例中,我仅更改Heat属性,并希望它基于该属性进行排序和筛选。在本例中,我只是在更改PushpinModel对象的Heat属性,并且期望ItemsSource为CollectionViewSource PlacesLocatedAndFiltered的ItemsControl根据Heat属性的更改进行排序和筛选。是的INotifyPropertyChanged
不会在ObservableCollection
上“级联”以成为集合更改通知。您可以创建一个ObservableCollection
的实现,该实现要求其所有项实现INotifyPropertyChange
,并且集合本身可能会导致属性更改成为集合更改……我假设您的建议是我将ObservableCollection子类化。你能描述一下我如何将属性更改为集合更改吗?谢谢,DevDigital。我看不到CollectionViewSource有刷新方法。我该如何称呼它?您需要从集合视图源获取集合视图。有关详细信息,请参阅。感谢大家的帮助。是的,在你的答案和网站上其他地方的答案之间。我通过创建ObservableCollection的子类来解决这个问题,该子类监视它所包含的对象中的属性更改。具体实现可在上找到。
<ItemsControl x:Name="ItemList" ItemsSource="{Binding Source={StaticResource placesSortedAndFiltered}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Placename}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<CollectionViewSource x:Key="placesSortedAndFiltered" Source="{Binding ElementName=MyMainPage, Path=Pushpins}" Filter="PlaceHeat_Filter">
<CollectionViewSource.SortDescriptions>
<componentmodel:SortDescription PropertyName="Placename" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
private void PlaceHeat_Filter(object sender, FilterEventArgs e)
{
e.Accepted = (((PushpinModel)e.Item).Heat != Heat.na);
}