在LINQ to SQL中的自定义属性上提升PropertyChanged

时间:2018-04-07 20:49:44

标签: wpf linq-to-sql observablecollection inotifypropertychanged

我在数据库中有一个带有三个Foriegn Keys(BrandID,TypeID,SupplierID)的产品表。我正在使用LINQ to SQL,它使用INotifyPropertyChanged自动创建类和属性。我想在Brand Class中添加自己的自定义属性。因此,我扩展了部分Brand Class并在其中添加了自定义属性(NoOfProducts)。现在自定义属性工作正常,但是当我在特定产品中添加Brand时,它不会触发属性更改事件。 INotifyPropertyChanged在自定义属性中不起作用。

扩展部分品牌类:

public partial class Brand
{
    private int _NoOfProducts;

    //Get a number of products in Brands
    public int NoOfProducts
    {
        get
        {    
            _NoOfProducts = Products.Count(p => p.BrandID != null);

            return _NoOfProducts;
        }
        set
        {
            _NoOfProducts = value;
            SendPropertyChanged("NoOfProducts");
        }
    }
}

从LINQ到SQL designer.cs的品牌类

    [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Brands")]
public partial class Brand : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _BrandID;

    private string _BrandName;

    private System.DateTime _CreatedDate;

    private System.Nullable<System.DateTime> _LastUpdated;

    private EntitySet<Product> _Products;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnBrandIDChanging(int value);
partial void OnBrandIDChanged();
partial void OnBrandNameChanging(string value);
partial void OnBrandNameChanged();
partial void OnCreatedDateChanging(System.DateTime value);
partial void OnCreatedDateChanged();
partial void OnLastUpdatedChanging(System.Nullable<System.DateTime> value);
partial void OnLastUpdatedChanged();
#endregion

    public Brand()
    {
        this._Products = new EntitySet<Product>(new Action<Product>(this.attach_Products), new Action<Product>(this.detach_Products));
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_BrandID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int BrandID
    {
        get
        {
            return this._BrandID;
        }
        set
        {
            if ((this._BrandID != value))
            {
                this.OnBrandIDChanging(value);
                this.SendPropertyChanging();
                this._BrandID = value;
                this.SendPropertyChanged("BrandID");
                this.OnBrandIDChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_BrandName", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
    public string BrandName
    {
        get
        {
            return this._BrandName;
        }
        set
        {
            if ((this._BrandName != value))
            {
                this.OnBrandNameChanging(value);
                this.SendPropertyChanging();
                this._BrandName = value;
                this.SendPropertyChanged("BrandName");
                this.OnBrandNameChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CreatedDate", DbType="DateTime NOT NULL")]
    public System.DateTime CreatedDate
    {
        get
        {
            return this._CreatedDate;
        }
        set
        {
            if ((this._CreatedDate != value))
            {
                this.OnCreatedDateChanging(value);
                this.SendPropertyChanging();
                this._CreatedDate = value;
                this.SendPropertyChanged("CreatedDate");
                this.OnCreatedDateChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastUpdated", DbType="DateTime")]
    public System.Nullable<System.DateTime> LastUpdated
    {
        get
        {
            return this._LastUpdated;
        }
        set
        {
            if ((this._LastUpdated != value))
            {
                this.OnLastUpdatedChanging(value);
                this.SendPropertyChanging();
                this._LastUpdated = value;
                this.SendPropertyChanged("LastUpdated");
                this.OnLastUpdatedChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Brand_Product", Storage="_Products", ThisKey="BrandID", OtherKey="BrandID")]
    public EntitySet<Product> Products
    {
        get
        {
            return this._Products;
        }
        set
        {
            this._Products.Assign(value);
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void attach_Products(Product entity)
    {
        this.SendPropertyChanging();
        entity.Brand = this;
    }

    private void detach_Products(Product entity)
    {
        this.SendPropertyChanging();
        entity.Brand = null;
    }
}

从LINQ到SQL designer.cs的产品类

    [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Products")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _ProductID;

    private string _ProductName;

    private string _Barcode;

    private System.Nullable<int> _TypeID;

    private System.Nullable<int> _BrandID;

    private System.Nullable<int> _SupplierID;

    private decimal _CostPrice;

    private decimal _SellPrice;

    private int _Stock;

    private bool _Unavailable;

    private string _ImagePath;

    private System.DateTime _CreatedDate;

    private System.Nullable<System.DateTime> _LastUpdated;

    private EntitySet<OrderDetail> _OrderDetails;

    private EntityRef<Brand> _Brand;

    private EntityRef<ProductType> _ProductType;

    private EntityRef<Supplier> _Supplier;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnProductIDChanging(int value);
partial void OnProductIDChanged();
partial void OnProductNameChanging(string value);
partial void OnProductNameChanged();
partial void OnBarcodeChanging(string value);
partial void OnBarcodeChanged();
partial void OnTypeIDChanging(System.Nullable<int> value);
partial void OnTypeIDChanged();
partial void OnBrandIDChanging(System.Nullable<int> value);
partial void OnBrandIDChanged();
partial void OnSupplierIDChanging(System.Nullable<int> value);
partial void OnSupplierIDChanged();
partial void OnCostPriceChanging(decimal value);
partial void OnCostPriceChanged();
partial void OnSellPriceChanging(decimal value);
partial void OnSellPriceChanged();
partial void OnStockChanging(int value);
partial void OnStockChanged();
partial void OnUnavailableChanging(bool value);
partial void OnUnavailableChanged();
partial void OnImagePathChanging(string value);
partial void OnImagePathChanged();
partial void OnCreatedDateChanging(System.DateTime value);
partial void OnCreatedDateChanged();
partial void OnLastUpdatedChanging(System.Nullable<System.DateTime> value);
partial void OnLastUpdatedChanged();
#endregion

    public Product()
    {
        this._OrderDetails = new EntitySet<OrderDetail>(new Action<OrderDetail>(this.attach_OrderDetails), new Action<OrderDetail>(this.detach_OrderDetails));
        this._Brand = default(EntityRef<Brand>);
        this._ProductType = default(EntityRef<ProductType>);
        this._Supplier = default(EntityRef<Supplier>);
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int ProductID
    {
        get
        {
            return this._ProductID;
        }
        set
        {
            if ((this._ProductID != value))
            {
                this.OnProductIDChanging(value);
                this.SendPropertyChanging();
                this._ProductID = value;
                this.SendPropertyChanged("ProductID");
                this.OnProductIDChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductName", DbType="NVarChar(250) NOT NULL", CanBeNull=false)]
    public string ProductName
    {
        get
        {
            return this._ProductName;
        }
        set
        {
            if ((this._ProductName != value))
            {
                this.OnProductNameChanging(value);
                this.SendPropertyChanging();
                this._ProductName = value;
                this.SendPropertyChanged("ProductName");
                this.OnProductNameChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Barcode", DbType="NVarChar(200)")]
    public string Barcode
    {
        get
        {
            return this._Barcode;
        }
        set
        {
            if ((this._Barcode != value))
            {
                this.OnBarcodeChanging(value);
                this.SendPropertyChanging();
                this._Barcode = value;
                this.SendPropertyChanged("Barcode");
                this.OnBarcodeChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TypeID", DbType="Int")]
    public System.Nullable<int> TypeID
    {
        get
        {
            return this._TypeID;
        }
        set
        {
            if ((this._TypeID != value))
            {
                if (this._ProductType.HasLoadedOrAssignedValue)
                {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OnTypeIDChanging(value);
                this.SendPropertyChanging();
                this._TypeID = value;
                this.SendPropertyChanged("TypeID");
                this.OnTypeIDChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_BrandID", DbType="Int")]
    public System.Nullable<int> BrandID
    {
        get
        {
            return this._BrandID;
        }
        set
        {
            if ((this._BrandID != value))
            {
                if (this._Brand.HasLoadedOrAssignedValue)
                {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OnBrandIDChanging(value);
                this.SendPropertyChanging();
                this._BrandID = value;
                this.SendPropertyChanged("BrandID");
                this.OnBrandIDChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_SupplierID", DbType="Int")]
    public System.Nullable<int> SupplierID
    {
        get
        {
            return this._SupplierID;
        }
        set
        {
            if ((this._SupplierID != value))
            {
                if (this._Supplier.HasLoadedOrAssignedValue)
                {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OnSupplierIDChanging(value);
                this.SendPropertyChanging();
                this._SupplierID = value;
                this.SendPropertyChanged("SupplierID");
                this.OnSupplierIDChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CostPrice", DbType="Money NOT NULL")]
    public decimal CostPrice
    {
        get
        {
            return this._CostPrice;
        }
        set
        {
            if ((this._CostPrice != value))
            {
                this.OnCostPriceChanging(value);
                this.SendPropertyChanging();
                this._CostPrice = value;
                this.SendPropertyChanged("CostPrice");
                this.OnCostPriceChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_SellPrice", DbType="Money NOT NULL")]
    public decimal SellPrice
    {
        get
        {
            return this._SellPrice;
        }
        set
        {
            if ((this._SellPrice != value))
            {
                this.OnSellPriceChanging(value);
                this.SendPropertyChanging();
                this._SellPrice = value;
                this.SendPropertyChanged("SellPrice");
                this.OnSellPriceChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Stock", DbType="Int NOT NULL")]
    public int Stock
    {
        get
        {
            return this._Stock;
        }
        set
        {
            if ((this._Stock != value))
            {
                this.OnStockChanging(value);
                this.SendPropertyChanging();
                this._Stock = value;
                this.SendPropertyChanged("Stock");
                this.OnStockChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Unavailable", DbType="Bit NOT NULL")]
    public bool Unavailable
    {
        get
        {
            return this._Unavailable;
        }
        set
        {
            if ((this._Unavailable != value))
            {
                this.OnUnavailableChanging(value);
                this.SendPropertyChanging();
                this._Unavailable = value;
                this.SendPropertyChanged("Unavailable");
                this.OnUnavailableChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ImagePath", DbType="NVarChar(250)")]
    public string ImagePath
    {
        get
        {
            return this._ImagePath;
        }
        set
        {
            if ((this._ImagePath != value))
            {
                this.OnImagePathChanging(value);
                this.SendPropertyChanging();
                this._ImagePath = value;
                this.SendPropertyChanged("ImagePath");
                this.OnImagePathChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CreatedDate", DbType="DateTime NOT NULL")]
    public System.DateTime CreatedDate
    {
        get
        {
            return this._CreatedDate;
        }
        set
        {
            if ((this._CreatedDate != value))
            {
                this.OnCreatedDateChanging(value);
                this.SendPropertyChanging();
                this._CreatedDate = value;
                this.SendPropertyChanged("CreatedDate");
                this.OnCreatedDateChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastUpdated", DbType="DateTime")]
    public System.Nullable<System.DateTime> LastUpdated
    {
        get
        {
            return this._LastUpdated;
        }
        set
        {
            if ((this._LastUpdated != value))
            {
                this.OnLastUpdatedChanging(value);
                this.SendPropertyChanging();
                this._LastUpdated = value;
                this.SendPropertyChanged("LastUpdated");
                this.OnLastUpdatedChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Product_OrderDetail", Storage="_OrderDetails", ThisKey="ProductID", OtherKey="ProductID")]
    public EntitySet<OrderDetail> OrderDetails
    {
        get
        {
            return this._OrderDetails;
        }
        set
        {
            this._OrderDetails.Assign(value);
        }
    }

    [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Brand_Product", Storage="_Brand", ThisKey="BrandID", OtherKey="BrandID", IsForeignKey=true)]
    public Brand Brand
    {
        get
        {
            return this._Brand.Entity;
        }
        set
        {
            Brand previousValue = this._Brand.Entity;
            if (((previousValue != value) 
                        || (this._Brand.HasLoadedOrAssignedValue == false)))
            {
                this.SendPropertyChanging();

                if ((previousValue != null))
                {
                    this._Brand.Entity = null;
                    previousValue.Products.Remove(this);
                }
                this._Brand.Entity = value;
                if ((value != null))
                {
                    value.Products.Add(this);
                    this._BrandID = value.BrandID;
                }
                else
                {
                    this._BrandID = default(Nullable<int>);
                }
                this.SendPropertyChanged("Brand");


            }
        }
    }

    [global::System.Data.Linq.Mapping.AssociationAttribute(Name="ProductType_Product", Storage="_ProductType", ThisKey="TypeID", OtherKey="TypeID", IsForeignKey=true)]
    public ProductType ProductType
    {
        get
        {
            return this._ProductType.Entity;
        }
        set
        {
            ProductType previousValue = this._ProductType.Entity;
            if (((previousValue != value) 
                        || (this._ProductType.HasLoadedOrAssignedValue == false)))
            {
                this.SendPropertyChanging();
                if ((previousValue != null))
                {
                    this._ProductType.Entity = null;
                    previousValue.Products.Remove(this);
                }
                this._ProductType.Entity = value;
                if ((value != null))
                {
                    value.Products.Add(this);
                    this._TypeID = value.TypeID;
                }
                else
                {
                    this._TypeID = default(Nullable<int>);
                }
                this.SendPropertyChanged("ProductType");
            }
        }
    }

    [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Supplier_Product", Storage="_Supplier", ThisKey="SupplierID", OtherKey="SupplierID", IsForeignKey=true)]
    public Supplier Supplier
    {
        get
        {
            return this._Supplier.Entity;
        }
        set
        {
            Supplier previousValue = this._Supplier.Entity;
            if (((previousValue != value) 
                        || (this._Supplier.HasLoadedOrAssignedValue == false)))
            {
                this.SendPropertyChanging();
                if ((previousValue != null))
                {
                    this._Supplier.Entity = null;
                    previousValue.Products.Remove(this);
                }
                this._Supplier.Entity = value;
                if ((value != null))
                {
                    value.Products.Add(this);
                    this._SupplierID = value.SupplierID;
                }
                else
                {
                    this._SupplierID = default(Nullable<int>);
                }
                this.SendPropertyChanged("Supplier");
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void attach_OrderDetails(OrderDetail entity)
    {
        this.SendPropertyChanging();
        entity.Product = this;
    }

    private void detach_OrderDetails(OrderDetail entity)
    {
        this.SendPropertyChanging();
        entity.Product = null;
    }
}

我也试过这个:Raise PropertyChanged on custom properties in EntityObject

Brand DataGrid Image

0 个答案:

没有答案