半關于幾年前我的這篇文章:

我需要進行一些更改,以便我的資料網格中的總數在其他欄位更改時更新。這意味著我的解決方法(編輯按鈕)消失了,所以我試圖通過實作 INotifyPropertyChanged(我只是設定視圖主鍵)對部分 linq to sql 類進行一些修改,以獲取我用于計算的視圖。
我將其用作參考:datagrid WPF 中的計算列
如果我可以從此更改自動生成的類中 PaySum_Total getter 的行為,我就可以完成這項作業:
public decimal PaySum_Total_Pay
{
get
{
return this._PaySum_Total_Pay;
}
set
{
if ((this._PaySum_Total_Pay != value))
{
this.OnPaySum_Total_PayChanging(value);
this.SendPropertyChanging();
this._PaySum_Total_Pay = value;
this.SendPropertyChanged("PaySum_Total_Pay");
this.OnPaySum_Total_PayChanged();
}
}
對此:
public decimal PaySum_Total_Pay
{
get
{
return this.CalcTotalOnChange();
}
set
{
//same code here from earlier
}
}
but of course this gets removed as soon as I make any changes to the dbml. I have created another partial class in another file to provide code for some of the stub methods in the class here:
public partial class Payroll_SummaryReview : INotifyPropertyChanging, INotifyPropertyChanged
{
partial void OnPaySum_Total_AdditionsChanged()
{
this.OnPaySum_Total_PayChanged();
}
partial void OnPaySum_Total_PayChanged()
{
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_IsApprovedChanged()
{
//do something as approved cb changes...?
}
//partial void OnPaySum_Reason_DescriptionChanged();
partial void OnPaySum_MinApprovedChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_TrainerChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_OOTChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_MiscAdditionsChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_PayCorrectionChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_CallInLoadPayChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
partial void OnPaySum_CallInPayChanged()
{
CalcTotalOnChange();
this.SendPropertyChanged("PaySum_Total_Pay");
}
private decimal CalcTotalOnChange()
{
decimal total = 0m;
decimal minimum = this.PaySum_Shifts ?? 0 * this.Min_Rate;
bool minOk = (this.PaySum_Total_Extra_Pay ?? 0) this.PaySum_Total_Load_Pay (this.PaySum_MiscAdditions ?? 0) < minimum
&& this.PaySum_MinApproved
&& (this.PaySum_Total_Additions == null || this.PaySum_Total_Additions == 0);
if (minOk) //misc addition inclucisive of minimums.
this.PaySum_Total_Additions = minimum - (this.PaySum_Total_Extra_Pay ?? 0 this.PaySum_Total_Load_Pay this.PaySum_MiscAdditions ?? 0);
else
this.PaySum_Total_Additions = 0M;
total = this.PaySum_Total_Extra_Pay ?? 0
this.PaySum_Total_Load_Pay
this.PaySum_Total_Additions ?? 0
this.PaySum_OOT ?? 0
this.PaySum_Trainer ?? 0
this.PaySum_PayCorrection ?? 0
this.PaySum_MiscAdditions ?? 0
this.PaySum_CallInLoadPay ?? 0;
int intMo = this.PaySum_Week_End_Date.Month;
decimal callInRate = 0M;
if (intMo > 3 && intMo < 10)
callInRate = 100M;
else
callInRate = 85M;
total = (Convert.ToDecimal(this.PaySum_CallInPay) * callInRate);
//section determines eligibility for and amount of Seniority pay.... but I actually added this to the view so screw this section Payroll_SummaryReview.
//Need to add source to source control
this.PaySum_Seniority = total * this.SeniorityPercent ?? 0;
return = total this._PaySum_Seniority;
//PayrollDataContext.SavedSummary = false; //need to set unsaved changes flag in Review somewhow
}
It looks like I can't override the public decimal PaySum_Total_Pay because of the way Linq generates the property. Is there something else I'm not aware of to make this work? I also would be more than happy to approach this from a different angle but I'm not sure what that angle would be right now, but I would appreciate some help here.
Sorry if this is rambling a bit and ugly code, but still a WIP.
uj5u.com熱心網友回復:
您可以在處理事件的部分非自動生成類中實作默認建構式或方法PropertyChanged(我假設自動生成的SendPropertyChanged方法會引發),然后在事件處理程式中更新并引發PropertyChanged事件PaySum_Total_Pay。像這樣的東西:
public partial class Payroll_SummaryReview
{
public Payroll_SummaryReview()
{
this.PropertyChanged = Payroll_SummaryReview_PropertyChanged;
}
private void Payroll_SummaryReview_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(PaySum_Total_Pay))
{
this._PaySum_Total_Pay = ...;
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(nameof(PaySum_Total_Pay)));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436188.html
