Question

I am trying to use the calculated columns to display in my grid.

I have a partial class automatically generated by EF code generator with three properties:

Here is my code generated by EF entity generator

//------------------------------------------------------------------------------
// <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
}

}

It also works if i put OnPropertyChanged("Salary") in Hours,Wage,Overtime Property in EF Generated class (which is not a good idea) because if the class gets regenerated , my code will be wiped out

Any help is appreciated. (Sorry for the formatting , this is my first question)

Thanks

Was it helpful?

Solution 2

I was able to fix this issue. What was happening is that service was marshaling the data back to the UI but it was not marshaling the events with the properties, so what i had to do was to call the init method from the UI and it started working.

OTHER TIPS

Final Draft

(I removed old edits due to irrelevance)

In your partial class add, in addition to your definition of Salary:

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

private void NotifySalary(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Wages" || e.PropertyName == "Hours" || e.PropertyName == "Overtime")
    {
        OnPropertyChanged("Salary");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top