1

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

我有一个由 EF 代码生成器自动生成的部分类,具有三个属性:

这是我由 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 Generated 类中输入 Hours,Wage,Overtime 属性(这不是一个好主意),它也可以工作,因为如果类重新生成,我的代码将被清除

任何帮助表示赞赏。(对不起格式,这是我的第一个问题)

谢谢

4

2 回答 2

0

最终稿

(由于不相关,我删除了旧的编辑)

在您的部分课程中添加,除了您对薪水的定义:

partial void Init()
{
    _propertyChanged += NotifySalary;
}

private void NotifySalary(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Wages" || e.PropertyName == "Hours" || e.PropertyName == "Overtime")
    {
        OnPropertyChanged("Salary");
    }
}
于 2013-01-13T19:54:30.127 回答
0

我能够解决这个问题。发生的事情是服务将数据封送回 UI,但它没有用属性封送事件,所以我必须做的是从 UI 调用 init 方法并开始工作。

于 2013-01-23T19:02:28.667 回答