【问题标题】:Why is not ICollectionView refreshed?为什么 ICollectionView 没有刷新?
【发布时间】:2012-10-16 09:58:11
【问题描述】:

我不知道为什么我的 ICollectionView 没有刷新。谁能解释我做错了什么?

我做了一个这样的视图模型:

class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Entity> m_entities = new ObservableCollection<Entity>();
    public ICollectionView EntitiesView { get; private set; }
    public ICollectionView HighCountView { get; private set; }

    public ViewModel()
    {
        m_entities.Add(new Entity() { Id = 1, Name = "Erik", Description = "The first" });
        m_entities.Add(new Entity() { Id = 2, Name = "Olle", Description = "The second" });
        m_entities.Add(new Entity() { Id = 3, Name = "Kim", Description = "The last" });


        EntitiesView = CollectionViewSource.GetDefaultView(m_entities);
        EntitiesView.CurrentChanged += new EventHandler(EntitiesView_CurrentChanged);

        HighCountView = new CollectionView(m_entities);
        using (HighCountView.DeferRefresh())
        {
            HighCountView.Filter = e => ((Entity)e).Count > 3;
        }

    }

    private void EntitiesView_CurrentChanged(object sender, EventArgs e)
    {
        Entity current = EntitiesView.CurrentItem as Entity;
        if(current!=null)
        {
            current.Count++;
            HighCountView.Refresh();            // Do I need this line?
            OnPropertyChanged("HighCountView"); // or this?
        }
    }

...在我的窗口中,我将其用作数据上下文,如下所示:

public partial class MainWindow : Window
{
    private ViewModel vm = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = vm;
    }
}

...我正在像这样在 XAML 代码中进行绑定:

    <ListBox Grid.Column="0" x:Name="listView1" DisplayMemberPath="Name" ItemsSource="{Binding EntitiesView}" IsSynchronizedWithCurrentItem="True" />
    <ListView Grid.Column="1" x:Name="listView2" DisplayMemberPath="Name" ItemsSource="{Binding HighCountView}" IsSynchronizedWithCurrentItem="True" />

问题是所有三个实体总是显示在 listView2 中,尽管我设置了过滤器属性。为什么?

编辑

为了使示例完整,这里是实体类。

class Entity : INotifyPropertyChanged
{
    private int m_id;
    public int Id
    {
        bla bla.....
    }

    private string m_name;
    public string Name
    {
        bla bla.....
    }

    private string m_description;
    public string Description
    {
        bla bla.....
    }


    private int m_count;
    public int Count
    {
        get { return m_count; }
        set
        {
            if (value != m_count)
            {
                m_count = value;
                OnPropertyChanged("Count");
            }
        }
    }

    public void Update()
    {
        Description = "Updated: " + (++Count).ToString() + " times.";
    }

【问题讨论】:

  • 请添加Entity类的代码
  • 现在是添加实体类。
  • 你可以试试这个:var savedhighcount = HighCountView; HighCountView = null; OnPropertyChanged("HighCountView"); HighCountView = 保存的高计数; OnPropertyChanged("HighCountView"); .....仅在底部的构造函数中执行此操作....而不是在您的 CurrentChanged 中。
  • 没有任何区别... :(
  • 我不知道是不是这个问题,但一般来说你不会直接实例化一个CollectionView。相反,您应该创建一个新的 CollectionViewSource 实例并获取相应的视图。让引擎决定使用 HighCountView = new CollectionViewSource{Source = m_entities}.View; 的最佳实现是什么

标签: wpf data-binding icollectionview


【解决方案1】:

我终于找到了问题所在。

如果我换行:

    HighCountView = new CollectionView(m_entities);

到这里

    HighCountView = new ListCollectionView(m_entities);

然后它按预期工作。

我也可以去掉这行

        OnPropertyChanged("HighCountView"); // or this?

我希望这可以帮助别人!

【讨论】:

  • 它有帮助! :)
猜你喜欢
  • 2010-11-16
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 2016-10-22
  • 1970-01-01
  • 2013-07-18
  • 2018-08-06
相关资源
最近更新 更多