我有這個驗證規則:
public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
DteqModel Dteq = (value as BindingGroup).Items[0] as DteqModel;
if (CheckCircularReference(Dteq, Dteq.FedFrom)) {
return new ValidationResult(false, "Equipment cannot be fed from itself");
}
return new ValidationResult(true, null);
}
這在 DataGrid 上作為 RowValidaiton 應用,它可以正常作業,但不是 100% 預期的。 Dteq.FedFrom是一個組合框,驗證作用于行/單元格中的前一個值,而不是我當前在 RowEndEdit 之后輸入的值。似乎驗證是在將值保存到實際集合物件之前完成的。我在所有物件上使用 NotifyProperty 更改,并且資料網格中的集合是一個可觀察的集合。
DteqModel Dteq = (value as BindingGroup).Items[0]
我如何讓上面的行讀取當前在行中的資料?現在,如果我輸入了無效的結果,我必須兩次進入編輯模式才能使失敗的驗證生效。在那之后,我無法將其改回任何有效的內容,因為它總是導致驗證失敗并且不會更新該值。
作為參考,CheckCircularReference只是直接或通過父/子關系的完整樹檢查Dteq.Tag == Dteq.FedFrom。
private bool CheckCircularReference(DteqModel startDteq, string nextDteq, int counter =1) {
var dteqDict = Dictionaries.dteqDict;
if (nextDteq == "" & counter == 1) { // sets the initial FedFrom
nextDteq = startDteq.FedFrom;
}
if (dteqDict.ContainsKey(nextDteq) == false || nextDteq == "" || counter > dteqDict.Count) { // all equipment has been checked
return false;
}
else if (startDteq.FedFrom == startDteq.Tag) { // if the equipment is fed from itself
return true;
}
else if (dteqDict.ContainsKey(nextDteq)) {
if (dteqDict[nextDteq].FedFrom == startDteq.Tag) { // if the equipment is indirectly fed from itself
return true;
}
counter = 1;
return CheckCircularReference(startDteq, dteqDict[nextDteq].FedFrom, counter); // if not increase the count and check the next Equipment
}
return false;
}
提前致謝。
uj5u.com熱心網友回復:
我不得不向我的 XAML 添加驗證步驟。
具有錯誤行為的 XAML:
<DataGrid.RowValidationRules>
<rules:InvalidFedFromRule ValidatesOnTargetUpdated="True"/>
</DataGrid.RowValidationRules>
具有正確行為的 XAML:
<DataGrid.RowValidationRules>
<rules:InvalidFedFromRule ValidatesOnTargetUpdated="True" ValidationStep="CommittedValue"/>
</DataGrid.RowValidationRules>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381117.html
上一篇:MSI安裝程式所有用戶:設定SCRNSAVE.EXE的值
下一篇:如何更改串列框中的所有專案
