ItemsControl 在页面首次加载后不会执行或重新过滤
我正在创建一个带有天气图的应用程序,该地图将不同地方的热量显示为图钉。为此,由支持 INotifyPropertyChanged
接口的自己的 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);
}
其中公共枚举 Heat {na,cool,normal,warm,hot}
问题是 ItemsControl 列表在页面加载时显示适当排序和过滤,但在更改 PushpinModel 对象属性时不会更新。请注意,当 Pushpins 对象绑定到 Bing 地图控件时,PushpinModel 对象会按预期更新。因此,不知何故,我的 ItemsControl 列表没有更新,即使它通过 CollectionView 绑定到 ObservableCollection
I'm creating an application with a weather map that shows the heat as a Pushpin at different places. To do this, supplied by own PushpinModel
that supports the INotifyPropertyChanged
interface:
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
}
The PushpinModel
objects are contained in an ObservableCollection
called Pushpins which are periodically updated to 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
}
I display the Pushpins on a Bing Map, but also as items in an ItemsControl:
<ItemsControl x:Name="ItemList" ItemsSource="{Binding Source={StaticResource placesSortedAndFiltered}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Placename}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The ItemsSource is defined as a 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>
with a Filter in the codebehind defined as:
private void PlaceHeat_Filter(object sender, FilterEventArgs e)
{
e.Accepted = (((PushpinModel)e.Item).Heat != Heat.na);
}
where a public enum Heat {na,cool,normal,warm,hot}
The problem is that ItemsControl list display appropriately sorted and filtered on page load, but does NOT update when the PushpinModel objects properties are changed. Please note that when the Pushpins object is bound to a Bing Map Control, the PushpinModel objects do update as expected. So somehow, my ItemsControl list isn't updating even though it is bound via a CollectionView to an ObservableCollection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
CollectionViewSource
实现仅在底层集合更改(或重置)时支持自动过滤。如果基础数据项的属性发生更改,则不会调用过滤器。当集合中某个项目的属性发生更改时,您可以在
CollectionViewSource
上调用Refresh()
,也可以实现自己的CollectionViewSource
侦听基础数据对象上的属性更改事件,或者您可以直接绑定到经过筛选(和排序)的集合,而不是使用CollectionViewSource
。I believe that the
CollectionViewSource
implementation only supports automatic filtering when the underlying collection is changed (or reset). The filter does not get invoked if a property of an underlying data item changes.You could either call
Refresh()
on theCollectionViewSource
when a property for an item in the collection changes, or you could implement your ownCollectionViewSource
which listens to property changed events on underlying data objects, or you could bind directly to a filtered (and sorted) collection rather than using theCollectionViewSource
.