Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create calculated Fields in Partial Classes - WPF
    text
    copied!<p>I am trying to use the calculated columns to display in my grid.</p> <p>I have a partial class automatically generated by EF code generator with three properties: and i am trying to creating another partial class and add calculated field there for e.g. </p> <pre><code>Public partial class Employee { public decimal? totalSalary { get { return salary*wagerate+bonus; } } } </code></pre> <p>It works fine for the first time but does not work when the salary/bonus/hours are changed. I am using these fields inside a grid </p> <p>Here is my code generated by EF entity generator</p> <pre><code>//------------------------------------------------------------------------------ // &lt;auto-generated&gt; // This code was generated from a template. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // &lt;/auto-generated&gt; //------------------------------------------------------------------------------ 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&lt;decimal&gt; Salary { get { return _salary; } set { if (_salary != value) { _salary = value; OnPropertyChanged("Salary"); } } } private Nullable&lt;decimal&gt; _salary; [DataMember] public Nullable&lt;decimal&gt; WageRate { get { return _wageRate; } set { if (_wageRate != value) { _wageRate = value; OnPropertyChanged("WageRate"); } } } private Nullable&lt;decimal&gt; _wageRate; [DataMember] public Nullable&lt;decimal&gt; Bonus { get { return _bonus; } set { if (_bonus != value) { _bonus = value; OnPropertyChanged("Bonus"); } } } private Nullable&lt;decimal&gt; _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 &amp;&amp; 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 } </code></pre> <p>}</p> <p>It also works if i put <code>OnPropertyChanged("Salary")</code> 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</p> <p>Any help is appreciated. (Sorry for the formatting , this is my first question)</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload