可观察集合不会触发setter或更新C#wpf

时间:2016-01-11 23:46:23

标签: c# wpf

我不完全确定为什么这不会更新。因此,在我的应用程序中,您可以使用文本框修改右侧的用户名。这些变化实时反映在左侧。当您点击“添加新许可证”时,按钮,它会创建一个新许可证并将其添加到选定的客户。然而,列表视图列&Lincenes'似乎没有更新,以反映客户拥有的许可证数量。作为测试,我在这里的Obs集合中放置了一个print语句

        private ObservableCollection<License> licenses;
        public ObservableCollection<License> Licenses
        {
            get { return licenses ?? (licenses = new ObservableCollection<License>()); }
            set
            {
                Console.Write("Modified");
                Set(ref licenses, value);
                RaisePropertyChanged(nameof(LicensesCount));
            }
        }

然而,我注意到它由于某种原因永远不会被打印出来。我很擅长做什么或改变什么。以下是我的代码的主要部分。 customer和License类对象都是基类INotify ...

enter image description here enter image description here

Customer.cs和License.cs类

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Input;
using WpfApplication1.Helper;

namespace WpfApplication1.Model
{
    public class Customer : NotifyBase
    {
        private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set { Set(ref firstName, value); }
        }

        public string LicensesCount
        {
            get { return this.Licenses.Count.ToString(); }
        }

        private ObservableCollection<License> licenses;
        public ObservableCollection<License> Licenses
        {
            get { return licenses ?? (licenses = new ObservableCollection<License>()); }
            set
            {
                Console.Write("Modified");
                Set(ref licenses, value);
                RaisePropertyChanged(nameof(LicensesCount));
            }
        }

        public Customer(string firstname, string lastname, string email, string company)
        {
            this.FirstName = firstname;
        }

        // Commands
        private ICommand addNewLicense_Command;
        public ICommand AddNewLicense_Command
        {
            get { return addNewLicense_Command ?? (addNewLicense_Command = new RelayCommand<Customer>(n =>{ AddNewLicense_Execute(n); }));}
        }

        public void AddNewLicense_Execute(Customer customer)
        {
            Licenses.Add(new License("Paint"));
        }
    }


    public class License : NotifyBase
    {
        private string product;
        public string Product
        {
            get { return product; }
            set { Set(ref product, value); }
        }

        public License(string product)
        {
            this.Product = product;
        }

    }

}

NotifyBase.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;


namespace WpfApplication1.Helper
{
    public abstract class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            RaisePropertyChanged(propertyName);
            return true;
        }
    }
}

以下是解决方案的链接:Solution Files

2 个答案:

答案 0 :(得分:1)

在您的代码中,当您将新的LicensesCount设置为ObservableCollection属性时,您正在更新Licenses。添加新License对象时Licenses属性不会更改。要正确更新LicensesCount,您应该收听CollectionChanged可观察集合的licenses事件。

它应该看起来像这样:

private ObservableCollection<License> licenses;
public ObservableCollection<License> Licenses
{
    get { return licenses ?? (licenses = CreateLicensesCollection()); }
    set
    {
        Console.Write("Modified");
        Set(ref licenses, value);
        RaisePropertyChanged(nameof(LicensesCount));
    }
}

private ObservableCollection<License> CreateLicensesCollection()
{
    var collection = new ObservableCollection<License>();
    collection.CollectionChanged += (s, a) => RaisePropertyChanged(nameof(LicensesCount));
    return collection;
}

答案 1 :(得分:0)

为什么不直接绑定到ObservableCollection的Count属性?将xaml中的绑定源从LicensesCount更改为Licenses.Count。由于ObservableCollection具有INotifyPropertyChanged的内置功能,因此无需额外编码。