如何在部分类中创建计算字段



我正在尝试使用计算列显示在我的网格。

我有一个由EF代码生成器自动生成的部分类,具有三个属性:我正试图创建另一个部分类,并在那里添加计算字段,例如

Public partial class Employee
{
public decimal? totalSalary
{
get
{
return salary*wagerate+bonus;
}
}
}

第一次可以正常工作,但当工资/奖金/工作时间改变时就不工作了。我在网格中使用这些字段

这是我的代码生成EF实体生成器

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
namespace Employees.Contract
{
[DataContract(IsReference = true)]
[KnownType(typeof(Department))]
[KnownType(typeof(PropertyType))]
public partial class Employee: IObjectWithChangeTracker, INotifyPropertyChanged,IDataErrorInfo
{
    [NonSerialized]
    private CLOS.Contract.Validation.DataErrorInfoSupport dataErrorInfoSupport;
    public Employee()
    {            
        dataErrorInfoSupport = new CLOS.Contract.Validation.DataErrorInfoSupport(this);
        Init();
    }
    partial void Init();
    string IDataErrorInfo.Error { get { return dataErrorInfoSupport.Error; } }
    string IDataErrorInfo.this[string memberName] { get { return dataErrorInfoSupport[memberName]; } }
    #region Primitive Properties

    [DataMember]
    public Nullable<decimal> Salary
    {
        get { return _salary; }
        set
        {
            if (_salary != value)
            {
                _salary = value;
                OnPropertyChanged("Salary");
            }
        }
    }
    private Nullable<decimal> _salary;
    [DataMember]
    public Nullable<decimal> WageRate
    {
        get { return _wageRate; }
        set
        {
            if (_wageRate != value)
            {
                _wageRate = value;
                OnPropertyChanged("WageRate");
            }
        }
    }
    private Nullable<decimal> _wageRate;
    [DataMember]
    public Nullable<decimal> Bonus
    {
        get { return _bonus; }
        set
        {
            if (_bonus != value)
            {
                _bonus = value;
                OnPropertyChanged("Bonus");
            }
        }
    }
    private Nullable<decimal> _bonus;
    #endregion
    #region Navigation Properties
    [DataMember]
    public Department Department
    {
        get { return _department; }
        set
        {
            if (!ReferenceEquals(_department, value))
            {
                var previousValue = _department;
                _department = value;
                OnNavigationPropertyChanged("Department");
            }
        }
    }
    private Borrower _department;
    [DataMember]
    public PropertyType PropertyType
    {
        get { return _propertyType; }
        set
        {
            if (!ReferenceEquals(_propertyType, value))
            {
                var previousValue = _propertyType;
                _propertyType = value;
                OnNavigationPropertyChanged("PropertyType");
            }
        }
    }
    private PropertyType _propertyType;
    #endregion
    #region ChangeTracking
    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
        {
            ChangeTracker.State = ObjectState.Modified;
        }
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    protected virtual void OnNavigationPropertyChanged(String propertyName)
    {
        if (_propertyChanged != null)
        {
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged{ add { _propertyChanged += value; } remove { _propertyChanged -= value; } }
    private event PropertyChangedEventHandler _propertyChanged;
    private ObjectChangeTracker _changeTracker;
    [DataMember]
    public ObjectChangeTracker ChangeTracker
    {
        get
        {
            if (_changeTracker == null)
            {
                _changeTracker = new ObjectChangeTracker();
                _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
            }
            return _changeTracker;
        }
        set
        {
            if(_changeTracker != null)
            {
                _changeTracker.ObjectStateChanging -= HandleObjectStateChanging;
            }
            _changeTracker = value;
            if(_changeTracker != null)
            {
                _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
            }
        }
    }
    private void HandleObjectStateChanging(object sender, ObjectStateChangingEventArgs e)
    {
        if (e.NewState == ObjectState.Deleted)
        {
            ClearNavigationProperties();
        }
    }
    protected bool IsDeserializing { get; private set; }
    [OnDeserializing]
    public void OnDeserializingMethod(StreamingContext context)
    {
        IsDeserializing = true;
    }
    [OnDeserialized]
    public void OnDeserializedMethod(StreamingContext context)
    {
        dataErrorInfoSupport = new CLOS.Contract.Validation.DataErrorInfoSupport(this);
        IsDeserializing = false;
        ChangeTracker.ChangeTrackingEnabled = true;
    }
    protected virtual void ClearNavigationProperties()
    {
        Department = null;
        PropertyType = null;
    }
    #endregion
}

}

它也可以工作,如果我把OnPropertyChanged("Salary")在小时,工资,加班属性在EF生成类(这不是一个好主意),因为如果类得到再生,我的代码将被清除

任何帮助都是感激的。(抱歉格式,这是我的第一个问题)

谢谢

在partial类中,使用partial Init方法订阅PropertyChanged事件,当工资、工资或奖金属性发生变化时,通知客户端总工资的变化。

这样您就不需要修改生成的代码。(这就是为什么Init方法是局部的)。

public partial class Employee
{
    partial void Init()
    {
        _propertyChanged += PropertyChangedHandler;
    }
    void PropertyChangedHandler(object sender, PropertyChangedEventArgs args)
    {
        if(args.PropertyName == "salary" ||
           args.PropertyName == "wagerate" ||
           args.PropertyName == "bonus")
         {
             OnPropertyChanged("totalSalary");
         }
    }
    public decimal? totalSalary
    {
        get
        {
            return salary * wagerate + bonus;
        }
    }
}

这就是为什么MVVM是一个流行的设计模式,如果你把你的雇员(一个模型)包装在一个新的类(一个ViewModel)中,在你把它交给网格(视图)之前,它将更容易定制。

然而,让你的当前代码工作的一个简单的方法是在你的部分类中附加当前PropertyChanged事件,如果当前属性名匹配一个依赖属性(注意递归!),就调用OnPropertyChanged("Salary")

相关内容

  • 没有找到相关文章

最新更新