BindingGroup.GetValue(Object, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回指定之屬性和項目的建議值。
public:
System::Object ^ GetValue(System::Object ^ item, System::String ^ propertyName);
public object GetValue(object item, string propertyName);
override this.GetValue : obj * string -> obj
Public Function GetValue (item As Object, propertyName As String) As Object
參數
- item
- Object
包含指定屬性的物件。
- propertyName
- String
該房產的提議價值。
傳回
建議的房產價值。
例外狀況
指定物品和屬性沒有約束。
指定屬性的值無法取得,原因可能是轉換錯誤或先前驗證規則失敗。
範例
以下範例是一個應用程式的一部分,該應用程式會提示使用者輸入多個客戶,並為每位客戶指派一位銷售代表。 申請會檢查銷售代表與顧客是否屬於同一地區。 範例展示了 Validate 該方法,利用該 GetValue(Object, String) 方法取得客戶輸入的數值。
public class AreasMatch : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
BindingGroup bg = value as BindingGroup;
Customer cust = bg.Items[0] as Customer;
if (cust == null)
{
return new ValidationResult(false, "Customer is not the source object");
}
Region region = (Region)bg.GetValue(cust, "Location");
ServiceRep rep = bg.GetValue(cust, "ServiceRepresentative") as ServiceRep;
string customerName = bg.GetValue(cust, "Name") as string;
if (region == rep.Area)
{
return ValidationResult.ValidResult;
}
else
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} must be assigned a sales representative that serves the {1} region. \n ", customerName, region);
return new ValidationResult(false, sb.ToString());
}
}
}
Public Class AreasMatch
Inherits ValidationRule
Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As ValidationResult
Dim bg As BindingGroup = TryCast(value, BindingGroup)
Dim cust As Customer = TryCast(bg.Items(0), Customer)
If cust Is Nothing Then
Return New ValidationResult(False, "Customer is not the source object")
End If
Dim region As Region = CType(bg.GetValue(cust, "Location"), Region)
Dim rep As ServiceRep = TryCast(bg.GetValue(cust, "ServiceRepresentative"), ServiceRep)
Dim customerName As String = TryCast(bg.GetValue(cust, "Name"), String)
If region = rep.Area Then
Return ValidationResult.ValidResult
Else
Dim sb As New StringBuilder()
sb.AppendFormat("{0} must be assigned a sales representative that serves the {1} region. " & vbLf & " ", customerName, region)
Return New ValidationResult(False, sb.ToString())
End If
End Function
End Class
備註
在方法 ValidationRule.Validate 中使用此方法,取得將要提交給原始碼的值。 回報值的類型取決於該現象 ValidationRule 發生的階段。 例如,若 a TextBox 綁定於一個型別為整數的屬性,且ValidationRule該呼叫GetValue(Object, String)的設定ValidationStep為 RawProposedValue ,該方法回傳一個字串。 若 設定ValidationRuleValidationStep為 ConvertedProposedValue,該方法回傳綁定轉換器所回傳的類型。 在這個例子中,通常 GetValue(Object, String) 回傳一個整數。