【问题标题】:INotifyPropertyChanged for Count property in WPF?WPF中的Count属性的INotifyPropertyChanged?
【发布时间】:2014-06-12 22:13:49
【问题描述】:

我有一个文本框,它绑定到我的视图模型中的 Int 属性,该属性的计数为 ObservableCollection。这是我的 Xaml 绑定:

Text="{Binding ArticleCount, Mode=OneWay}" 

还有我的 int 属性:

 private Int32 _ArticleCount;
    public int ArticleCount
    {
        get
        {                
            if (this.ModelviewArticleObservableList == null)
            {
                return 0;
            }
            else
            {
                return this.ModelviewArticleObservableList.Count;                  
            }               
        } 
    }

这在应用程序初始化时效果很好,我在我的 UI TextBox 中获得了 ObservableCollection 计数。但是,有一些方法可以更改ObservableCollection,因此计数会更改。我需要一个 Property Changed 事件来通知计数何时更改,但我不确定如何执行此操作?这是我的ObservableCollection 属性:

 private ObservableCollection<viewArticle> _ModelviewArticleObservableList = new ObservableCollection<viewArticle>();
    public ObservableCollection<viewArticle> ModelviewArticleObservableList
    {
        get { return _ModelviewArticleObservableList; }
        set
        {
            _ModelviewArticleObservableList = value;
            OnPropertyChanged("ModelviewArticleObservableList");                
        }
    }

我目前有一个正常的INPC实现:

public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

【问题讨论】:

  • 您可以订阅ObservableCollection&lt;T&gt;CollectionChanged事件并从那里调用OnPropertyChanged("ArticleCount")

标签: c# wpf mvvm inotifypropertychanged


【解决方案1】:

ModelviewArticleObservableList 是您的 ViewModel 的公开可见属性吗?

如果是这样,您可以将 TextBox 直接绑定到它的 Count 属性。

Text="{Binding Path=ModelviewArticleObservableList.Count}" 

您需要“Path=”,因为您没有绑定到直接属性。 “Mode=OneWay”是不必要的。

ObservableCollection 实现 INotifyPropertyChanged,每次更新集合都会通知 Count 属性。

顺便说一句,你为什么使用 TextBox 而不是 TextBlock 来表示一个无法在 UI 中编辑的值?

【讨论】:

  • 我无法明确绑定到 ObservableCollection 的计数,因为集合中有超过 7000 条记录并且面临内存问题。
【解决方案2】:

每次集合中的元素数量发生变化时,您都必须引发PropertyChanged 事件。一个好办法是订阅ObservableCollectionCollectionChanged事件。

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // you could examine e to check if there is an actual change in the count first
    OnPropertyChanged("ArticleCount");
}

此外,集合本身设置为null 或其他值,在这种情况下,您的ArticleCount 属性也应该更改。您还必须从旧集合中删除 CollectionChanged 事件处理程序并将其添加到新集合中。

public ObservableCollection<viewArticle> ModelviewArticleObservableList
{
    get { return _ModelviewArticleObservableList; }
    set
    {
        if (_ModelviewArticleObservableList != null)
        {
            _ModelviewArticleObservableList.CollectionChanged -= OnCollectionChanged;
        }

        _ModelviewArticleObservableList = value;

        if (_ModelviewArticleObservableList != null)
        {
            _ModelviewArticleObservableList.CollectionChanged += OnCollectionChanged;
        }

        OnPropertyChanged("ModelviewArticleObservableList");
        OnPropertyChanged("ArticleCount");
    }
}

【讨论】:

  • 我明白你在说什么,德克。我的问题是我的 ObservableCollection 的设置器永远不会被命中,因为它只是在读取值。如果我在 ObservableCollection 的 getter 中调用 collectionChanged 事件,它会为添加的每条记录而被调用...
  • @Hardgraf 如果您不需要设置器,则只需将其删除并订阅构造函数中的CollectionChanged 事件。我不明白你的第二点。为什么要在 getter 中引发事件?
  • 不,我不想将事件添加到 getter。需要提醒自己,它是 ViewModel 而不是 View 的构造函数!尽管每次计数都会触发事件,但仍然会出现内存问题,因此需要弄清楚...
猜你喜欢
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2011-04-02
  • 2013-08-12
  • 2019-04-04
  • 1970-01-01
相关资源
最近更新 更多