我正在创建一个带有天气图的应用程序,它将不同位置的热度显示为图钉。为此,由支持INotifyPropertyChanged接口的own 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对象包含在一个名为ObservableCollection的图钉中,该图钉定期更新为ShowWeather:
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
}我在Bing地图上显示图钉,但也在ItemsControl中显示项目:
<ItemsControl x:Name="ItemList" ItemsSource="{Binding Source={StaticResource placesSortedAndFiltered}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Placename}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>ItemsSource被定义为CollectionViewSource:
<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);
}其中一个公共枚举热{na,凉爽,正常,温暖,热}
问题是,ItemsControl列表在页面加载时显示适当的排序和过滤,但不会在PushpinModel对象属性更改时更新。请注意,当图钉对象绑定到Bing Map控件时,PushpinModel对象会按预期进行更新。所以不知何故,我的ItemsControl列表不会更新,即使它通过CollectionView绑定到ObservableCollection
发布于 2012-02-17 08:03:26
我相信CollectionViewSource实现只支持在底层集合发生更改(或重置)时自动过滤。如果基础数据项的属性发生更改,则不会调用筛选器。
您可以在集合中某项的属性发生更改时在CollectionViewSource上调用Refresh(),也可以实现自己的CollectionViewSource来侦听基础数据对象上的属性更改事件,也可以直接绑定到经过筛选(和排序)的集合,而不是使用CollectionViewSource。
https://stackoverflow.com/questions/9320753
复制相似问题