首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第一次加载页面后,ItemsControl不重新排序或重新筛选

第一次加载页面后,ItemsControl不重新排序或重新筛选
EN

Stack Overflow用户
提问于 2012-02-17 07:32:35
回答 1查看 319关注 0票数 0

我正在创建一个带有天气图的应用程序,它将不同位置的热度显示为图钉。为此,由支持INotifyPropertyChanged接口的own PushpinModel提供:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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中显示项目:

代码语言:javascript
复制
 <ItemsControl x:Name="ItemList" ItemsSource="{Binding Source={StaticResource placesSortedAndFiltered}}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border>
                                    <TextBlock Text="{Binding Placename}" />
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

ItemsSource被定义为CollectionViewSource:

代码语言:javascript
复制
<CollectionViewSource  x:Key="placesSortedAndFiltered" Source="{Binding ElementName=MyMainPage, Path=Pushpins}" Filter="PlaceHeat_Filter">
    <CollectionViewSource.SortDescriptions>
        <componentmodel:SortDescription PropertyName="Placename" Direction="Ascending" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

代码中的筛选器定义为:

代码语言:javascript
复制
 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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-17 08:03:26

我相信CollectionViewSource实现只支持在底层集合发生更改(或重置)时自动过滤。如果基础数据项的属性发生更改,则不会调用筛选器。

您可以在集合中某项的属性发生更改时在CollectionViewSource上调用Refresh(),也可以实现自己的CollectionViewSource来侦听基础数据对象上的属性更改事件,也可以直接绑定到经过筛选(和排序)的集合,而不是使用CollectionViewSource

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9320753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档